[Javascript] onclick and functions

Rakesh Pai rakeshpai at gmail.com
Mon Aug 14 01:24:48 CDT 2006


On 8/12/06, Paul Novitski <paul at juniperwebcraft.com> wrote:
> At 07:30 AM 8/12/2006, Rakesh Pai wrote:
> >That said, using a addEvent function is a cleaner way to attach
> >handlers to an event.
>
>
> Rakesh,
>
> Can you please elaborate on this opinion?  Cleaner in what ways?
>
> Thanks,
> Paul

Sorry for the delayed response. Paul.

addEvent would generally be regarded as a better option for several
reasons. Firstly, it uses the vastly richer DOM Level 2 event
registeration technique. Secondly, you can chain together events, and
attach multiple event handlers to the same event using addEvent, which
is not possible using element.onclick, since setting a new handler for
element.onclick would remove the previous event handler. There are
more benefits, but I can't recollect them - they are largely low level
details. Unfortunately, since some browsers (read IE) have different
DOM Level 2 event registeration techniques, it is required to write a
wrapper to abstract the differences, which is what addEvent does.

If you do a google search for DOM Level 2 event handling or for
"addEvent" in Google, you should get a lot more information about the
event registration models, and about why using element.onclick is not
such a good idea.

-- 
Rakesh Pai
Mumbai, India.
rakesh.pai at gmail.com
http://piecesofrakesh.blogspot.com/



More information about the Javascript mailing list