emulating live event with pure js
http://stackoverflow.com/questions/9117607/how-can-i-attach-event-to-a-tag-which-is-in-string-form
HTML
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sed erat magna. Pellentesque at placerat arcu. Mauris sit amet erat massa, et iaculis enim. Cras elementum, urna id consequat dictum, turpis mauris porta sapien, et gravida nibh lectus ac arcu.
<div id="cnt"></div>
</div>
CSS
div {
margin-top: 10px;
}
#abc {
background-color: yellow;
}
JavaScript
function listen(evnt, elem, func) {
if (elem.addEventListener) {
elem.addEventListener(evnt, func, false);
}
else if (elem.attachEvent) {
var r = elem.attachEvent("on" + evnt, func);
return r;
}
else window.alert('I\'m sorry Dave, I\'m afraid I can\'t do that.');
}
var assignLiveEvent = function(id, evnt, callback) {
var handler = function(e) {
e = e || window.event;
e.target = e.target || e.srcElement;
if (e.target.id == id) {
//your code here
callback(e);
}
};
listen(evnt, document.body, handler);
};
var myVar = "<div id='abc'>some clickable text</div>";
assignLiveEvent("abc", "click", function(e) {
//your code here
alert("abc clicked");
});
// see? I'm inserting div after assigning the handler
document.getElementById('cnt').innerHTML = myVar;