0

I have this php code for $i from 1 to 19:

<tr id=\"comment$i\"><td colspan=\"2\" align=\"center\" style=\"color:black;\">COMMENTS</td></tr>

<tr id=\"hidden$i\" style=\"display: none;\"><td colspan=\"2\">FIRST COMMENT</td></tr>

I also have some javascript to change display of hidden$i when i click on comment$i:

$(document).ready(function(){
for (i = 0; i < 19; i++) { 
$("#comment" + i).click(function(){
    $("#hidden" + i).toggle();
});
}
});

Yet it is not working ... I must be failing to see something, i was wondering if you guys could help me out please.

Jackymamouth
  • 159
  • 2
  • 11
  • maybe it is missing var for start, `for(var i...` – Bek Jan 04 '16 at 08:10
  • did you tried to user toogleSlide() instead? – Aref Anafgeh Jan 04 '16 at 08:10
  • 1
    Try this: `$(document).ready(function() { for (i = 0; i < 19; i++) { $("#comment" + i).click((function(i) { return function() { $("#hidden" + i).toggle(); } })(i)); } });` By the time click event is being fired, value of `i` will be _length-1_ hence it will always work on last element-> `id='hidden18'` – Rayon Jan 04 '16 at 08:11
  • Do you strictly want to use 'for' loop. As you have included jQuery library you can easily achieve. – Gaurav Rai Jan 04 '16 at 08:12
  • 1
    at first you state `for $i from 1 to 19:` ... then your javascript is `for (i = 0; i < 19; i++)` ... probably not THE problem, but it is A problem – Jaromanda X Jan 04 '16 at 08:13
  • As @JaromandaX said, and another thing you have to keep in mind is `Do you add your table rows dynamically?` or you can also say `Does the jquery click events really get binded to the #comments?` – choz Jan 04 '16 at 08:15
  • @RayonDabre Perfect man, thx !! – Jackymamouth Jan 04 '16 at 08:21
  • @Jackymamouth, Cheers :) – Rayon Jan 04 '16 at 08:23

0 Answers0