I am trying to set the onclick event using javascript. The following code works:
var link = document.createElement(\'a\');
link.setAttribute(\'href\', \"#\");
l
You can add a DOM even listener with addEventListener(...)
, as David said. I've included attachEvent
for compatibility with IE.
var link = document.createElement('a');
link.setAttribute('href', "#");
if(link.addEventListener){
link.addEventListener('click', function(){
alert('clicked');
});
}else if(link.attachEvent){
link.attachEvent('onclick', function(){
alert('clicked');
});
}