<?php
for($i=0;$i<5;$i++){
?>
<button id="text<?php echo $i; ?>">hello </button>
<script>
var i=<?php echo $i; ?>;
$(document).ready(function(){
$("#text"+i).click(function(){
alert("hello");
})
})
</script>
<?php } ?>
If I have a varying id
like this and I want to call it in jQuery using this code its give me no result. Where is the problem? How can I call an element like this button
?
2
Answers
Would be better to move the script out of the loop, get the buttons all at once and then bind the click event:
I am satisfied with @Emre’s answer. And also remove
$(doucment).ready()
will solve your problem. Like this.