Dynamically assigned onclick event never executed when clicking link - Javascript / DHTML / Ajax
I just conquered an issue I was having with adynamically assigned onclick event never executed when clicking a link (click me for details). Essentially what I ended up coming up with and highly recommend to others is that if, for some insane crazy reason inexplicable even by cold Vulcan logic, your dynamically assigned event handler just WON'T go - do something like this:
for (var i=0;i<arr.length;i++)
{
var a = _bsn.DOM.createElement("div", { bgColor:"white" });
a.innerHTML = "<a href=\"#\" onclick=\"selVal(document.getElementById('"+pointer.fld.id+"'),"+arr[i]+");\">"+arr[i]+"</a>";
var li = _bsn.DOM.createElement( "li", {}, a );
ul.appendChild( li );
}
{
var a = _bsn.DOM.createElement("div", { bgColor:"white" });
a.innerHTML = "<a href=\"#\" onclick=\"selVal(document.getElementById('"+pointer.fld.id+"'),"+arr[i]+");\">"+arr[i]+"</a>";
var li = _bsn.DOM.createElement( "li", {}, a );
ul.appendChild( li );
}
Colorized by: CarlosAg.CodeColorizer That is to say, write out a div and fill the innerHTML (even though that a little off standard ;( ) with the element and explicitly write out the event handler. Then it will take for sure. Though that seems kinda common sense to me looking back on it, many hours of eye strain when into coming to that solution.
2 Comments:
Perhaps you should use some sort of "code to colorized-and-fixed-width-font html" convertor. It would make this a hundred time easier to read. ;)
Otherwise, excellent blog. :)
Thank you very much. I do need to get a code tool.
Post a Comment
<< Home