user1873574
user1873574

Reputation: 93

Click with append is not working

I have a problem with attaching an onclick event with an appended anchor. When I click on a row it is not giving me the iterative id.

for (var i = 0; i <= oRes.projects.length; i++) {
    jQuery('<a/>', {
        href: "#_",
        html: oRes.projects[i]['project_name'] + "<span>" + oRes.projects[i]['project_duration'] + "</span>" + i,
        class: "setup-button projectItems",
        "data-role": "button",
        "data-iconpos": "left",
        "data-projectId": oRes.projects[i]['project_id'],
        "click": function() {
            alert(i);
         }
    }).appendTo('#myprojectList');

    $('#myprojectList a').button();
}

Upvotes: 0

Views: 75

Answers (1)

RafH
RafH

Reputation: 4544

i will always be equal to your array.length, you can use the index of the button $(this).index() (same value as i iterates by 1 from 0 to array length).

[...]
"click": function() {
    alert( 'button:' + $(this).index()  );
 }

Demo here

Upvotes: 1

Related Questions