I have this code and it's javaScript and jQuery:
for (thisLooper = 1; thisLooper < 4; thisLooper++) {
if ($("#writeComments"+thisLooper).length > 0) {
$("#writeComments"+thisLooper+",#comments"+thisLooper+", #infoSpan"+thisLooper+"").hide();
$("#toggleCommentForm"+thisLooper).click(function () {
$("#writeComments+thisLooper").slideToggle();
});
}
}
What it does:
- Check if #writeComments1 exists
- If it exists, hide #writeComments1, #comments1 and #infoSpan1
- Then, if someone clicks on #toggleCommentForm1, slideToggle #writeComments1
- Do all this for #writeComments2 and it's friends
- Do all this for #writeComments3 and it's friends
With the code above nothing happens but if i replace:
$("#toggleCommentForm"+thisLooper).click(function () {
$("#writeComments+thisLooper").slideToggle();
});
width this:
$("#toggleCommentForm"+thisLooper).click(function () {
$("#writeComments1").slideToggle();
});
It all works but naturally only #writeComments1 slideToggles even if I click on #toggleCommentForm2 for instance.
My question is, why cant I use the variable "thisLooper" inside of the click events?