i am trying to get id names from a array and trying to assign id names to each p tag which are append dynamically when i click the button, html is:
<html>
<body>
<div id="container">
<button id="button">Add new div</button>
</div>
</body>
</html>
script is :
$(document).ready(function () {
$('#button').click(function(){
var data = [{'id':'user1'},{'id':'user2'},{'id':'user3'},{'id':'user4'},{'id':'user5'},];
$.each(data, function(val) {
var light=$('<p></p>');
light.attr('id',+val.id);
light.text('one');
$("#container").append(light);
});
});
});
Change to:
Also see the updated jsfiddle.
=== UPDATE ===
Also see my updated jsfiddle.