Adding event handler using javascript doesnt work in IE -



Adding event handler using javascript doesnt work in IE -

i trying add together event handler on page through javascript. tried follows,

var span=document.getelementbyid("wd67"); span.setattribute("onchange","alert('hello');");

but though event handler attribute gets added, doesn't fire when page viewed in ie, in firefox works properly. how ie recognize it?

don't utilize attributes that. best way add together event handlers using addeventlistener (all modern browsers) or attachevent (ie<9). furthermore, utilize handler function reference (wrap alert('hello') in function).

a crossbrowser function add together handlers elements:

function addhandler(obj,htype,fn){ if (obj.addeventlistener){ obj.addeventlistener(htype, fn); } else { obj.attachevent('on'+htype,fn); } } // usage var myspan=document.getelementbyid("wd67"); addhandler(myspan,'change',function(){alert('hello')});

see also: this jsfiddle

javascript

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -