CodeIgniter Forums
value in a loop using jquery - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: value in a loop using jquery (/showthread.php?tid=23792)



value in a loop using jquery - El Forum - 10-21-2009

[eluser]marjune[/eluser]
eh, pipz my problem is in my jquery script, when i click the thing i cant get the exact value of the id from the database because only the id of the first record would apear

here my code in a loop



foreach($data as $data) {

<input type="hidden" id="hideid" value="<?php echo $data->id;?>">
<a class="topicans">&lt;?php echo $data->topic."</a>";?&gt;

}

and my jq script

$(function(){

$(".topicans").click(function(e){
e.preventDefault();
var topic_id = $('#hidden').val();
alert(topic_id);
});
});

or

$(function(){

$(".topicans").click(function(e){
e.preventDefault();
var topic_id = $('#hidden',this).val();
alert(topic_id);
});
});


value in a loop using jquery - El Forum - 10-21-2009

[eluser]kgill[/eluser]
Might have something to do with this:

foreach($data as $data) {

You're storing the 1st iteration of your loop in the value you're trying to loop through. Guess what happens to the rest of the values in there when you do that...


value in a loop using jquery - El Forum - 10-21-2009

[eluser]marjune[/eluser]
i change it

foreach($data as $field) {

&lt;input type=“hidden” id=“hideid” value=”&lt;?php echo $field-&gt;id;?&gt;”>
<a >&lt;?php echo $field->topic.”</a>”;?&gt;

}


value in a loop using jquery - El Forum - 10-21-2009

[eluser]Unknown[/eluser]
Your code in the FOREACH loop is repeating the same ID, correct?
Try using a variable as a counter and add it to the ID name as you go through the loop.
IDs have to be unique.

Also, your jquery code seems to not point to the ID (id='hideid') at all, but to the input type (type='hidden')

Hope this helps


value in a loop using jquery - El Forum - 10-21-2009

[eluser]marjune[/eluser]
but how can i recognized the IDs if i add a counter through the loop?