user2496520
user2496520

Reputation: 921

Jquery clickevent not working

I am using loop function to fill some elements into div. Each element of that divs have onClick function. Problem is that onclick function is not firing.

Here is my code:

var Source = "#boxcard"; 
var ImgSource = [

  "http://icons.iconarchive.com/icons/martin-berube/sport/96/Volleyball-icon.png",
  "http://icons.iconarchive.com/icons/reclusekc/kulo/96/Skull-1-icon.png"

];
function OpenCard() {

    var id = $(this).attr("id");
    alert (id);
}
for (var y = 1; y < 3 ; y++) {
    $.each(ImgSource, function(i, val) {
        $(Source).append("<div class='flipper' id='flipper" + y + i + "'> <div class='front'></div><div class='back'><img src=" + val + " /></div></div>");
        $("flipper" + y + i).click(OpenCard);

    });
}

Upvotes: 1

Views: 43

Answers (1)

Jane S
Jane S

Reputation: 1487

You're missing the leading # on your variable name:

$("#flipper" + y + i).click(OpenCard);

[Edit]

Additionally as someone suggested in the comments, you could simply use the class name after you've finished iterating:

$(".flipper").click(OpenCard);

Upvotes: 2

Related Questions