What's wrong with addlistener... how do i fire my event

Posted by KoolKabin on Stack Overflow See other posts from Stack Overflow or by KoolKabin
Published on 2010-03-24T07:29:17Z Indexed on 2010/03/24 7:33 UTC
Read the original article Hit count: 224

I am using the following functions to do my task. It works fine when cursor moves away from textbox but if i want to fire the same event from code say like next function i get error...

function addEvent( obj, type, fn ) { if (obj.addEventListener) { obj.addEventListener( type, fn, false ); } else if (obj.attachEvent) { obj["e"+type+fn] = fn; obj[type+fn] = function() { obj"e"+type+fn; } obj.attachEvent( "on"+type, obj[type+fn] ); } else { obj["on"+type] = obj["e"+type+fn]; } }

function addEventByName(ObjName, event, func){ MyEle = document.getElementsByName(ObjName); addEvent(MyEle[0], event, func); }

addEventByName("txtBox", 'blur', function(){ alert('hello'); });

function fire(){ x = document.getElementsByName('txtBox')[0]; x.blur(); //gives error x.onblur(); //gives error }

© Stack Overflow or respective owner

Related posts about javascript-events

Related posts about JavaScript