Reputation: 155
I am creating a list of images dynamically, following is the code
<?php
for ( $counter = 0; $counter < $obj1->id; $counter ++)
{ ?>
<tr><td> Week <?php echo($counter+1); ?>
</td><td><?php echo" <a href=\"#hiddenDiv\" rel=\"facebox\";>";?>
<img src="tue.png"/ id="image" value="<?php echo $counter+1;?>" >
</a></td></tr>
<?php } ?>
when the user clicks on the image a form opens. every image represents a week so i need to uniquely identify on which week user is inserting data. i need id of that image through which i can identify a week. On click at the image following function is called
function pc(){
var temp = document.getElementById("image");
alert(temp.value);
}
but i am getting "undefined". can somebody help me with this?
Thanks
Thanks in advance
Upvotes: 0
Views: 4460
Reputation: 2223
You will notice that I changed the id on the img tag, which means each new image will have the id of the current counter.
I also added an onclick event to the link, and pass in the counter to the js function.
<?php
for ( $counter = 0; $counter < $obj1->id; $counter ++)
{ ?>
<tr><td> Week <?php echo($counter+1); ?>
</td><td><?php echo" <a href=\"#hiddenDiv\" onclick=\"pc(<?php echo $counter;?>)\" rel=\"facebox\";>";?>
<img src="tue.png"/ id="<?php echo $counter;?>" value="<?php echo $counter+1;?>" >
</a></td></tr>
<?php } ?>
Also, remove the quotes around "id" in your js function:
function pc(id){ var temp = document.getElementById(id); alert(temp.value); }
Upvotes: 2
Reputation: 8322
you have to pass id of the tag. like document.getElementById("image").
function pc(id){
var temp = document.getElementById("image");
alert(temp.value);
}
Upvotes: -1