0

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();
}
2
  • 2
    Well for one you're iterating oRes.projects.length + 1 times because of the <= in the for condition. Commented Nov 4, 2013 at 10:11
  • What do you mean by "iterative id" ? the value of i or projectId ? Commented Nov 4, 2013 at 10:31

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.