javascript - Giving ID to dynamically created button in jQuery -
i'm trying create set of buttons using jquery , give them id's, when try reference id's later, doesn't seem work. can fix it?
for(i=0;i<7;i++) { $("h2") .append('<input type="button" value="display answer">') .button() .attr("id", i); }; $("button").click(function() { if($(this).attr("id") === "all") { for(i=0;i<answerarr.length+1;i++){ $("#p" + i).text(answerarr[i-1]); } } else { $("#p" + $(this).attr("id")).text(answerarr[$(this).attr("id")-1]); } });
.append()
returns element called on, not newly appended element. adding id (and calling .button()
) on <h2>
element(s).
try using .appendto()
instead, , adding id when create element:
$('<input/>', { type: 'button', id: i, value: 'display answer' }).appendto('h2').button();
Comments
Post a Comment