jquery dynamically genearate element id in a loop

Posted by phil on Stack Overflow See other posts from Stack Overflow or by phil
Published on 2010-06-06T11:56:01Z Indexed on 2010/06/06 12:02 UTC
Read the original article Hit count: 177

Filed under:

This is a loop in a function intending to create elements <li> and give each <li> an unique id. But it's not working. I suspect it's a simple syntax error with the use of quote in .attr(). But I can't get a straight answer from Google.

for (i=0;i<array.length;i++)
{
//create HTML element of tag li
$('#suggest').append("<li></li>");
$("li").attr("id",'li'+i);
$('#li'+i).html(array[i]);
}

© Stack Overflow or respective owner

Related posts about jQuery