b_hole
04-30-2007, 01:11 PM
Hi,
I'm aware of 2 different methods to attach events to elements:
<div id="attach">text</div>
document.getElementById("attach").onclick=function () {// something}
<div id="attach">text</div>
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]( window.event ); }
obj.attachEvent( "on"+type, obj[type+fn] );
}
}
addEvent(document.getElementById("attach", "click", something);
I'm sure there's a difference, because after all - someone wrote addEvent function for a reason, so I guess in some cases - using onclick isn't the best thing to do.
What I don't understand is this: when should I use each one of these methods? when onclick is better and when addEvent function? what is the difference?
Thanks a lot.
I'm aware of 2 different methods to attach events to elements:
<div id="attach">text</div>
document.getElementById("attach").onclick=function () {// something}
<div id="attach">text</div>
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]( window.event ); }
obj.attachEvent( "on"+type, obj[type+fn] );
}
}
addEvent(document.getElementById("attach", "click", something);
I'm sure there's a difference, because after all - someone wrote addEvent function for a reason, so I guess in some cases - using onclick isn't the best thing to do.
What I don't understand is this: when should I use each one of these methods? when onclick is better and when addEvent function? what is the difference?
Thanks a lot.