value in a loop using jquery

#1
[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);
});
});

#2
[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...

#3
[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;

}

#4
[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

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.