I have created HTML elements dynamically in JavaScript with ids.
When getting value separately it works. But I used jQuery to handle the click operation which updates the database with id, and I am getting the id as undefined. The code is as follows.
Script:
var bt=document.createElement("b");
bt.setAttribute("class","assign");
var ah=document.createElement("a");
ah.href="";
ah.setAttribute('id',jsonobj[k]);
ah.id=jsonobj[k];
ah.innerHTML="Assign";
bt.appendChild(ah);
lb.appendChild(bt);
jQuery:
$('b.assign a').click(function(event) {
var id1 = $(this).attr('id');
alert(id1);
});
Try delegating the click handler, so it will automatically work for any newly-created tags:
http://api.jquery.com/on/