Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #8643

Re: <ul><li><ul><li>... onclick() only for one node ?

Message-ID <2567763.1uUHiEQOGO@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2011-11-25 20:04 +0100
Subject Re: <ul><li><ul><li>... onclick() only for one node ?
Newsgroups comp.lang.javascript
References <9j9pfhFe3lU1@mid.individual.net> <1834269.dtTrMRavdF@PointedEars.de> <9ja2cgFlpkU1@mid.individual.net> <4ecfda5e$0$7617$9b4e6d93@newsspool1.arcor-online.net>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


Martin Honnen wrote:

> Jörg Weule wrote:
>> Not I can write <div onClick()="doMyFunction(event.target)"> to get the
>> inner clicked element but Firefox is not accepting
>>
>> var l = document.createElement("li");
>> l.onclick = function(){ doMyFunction(event.target) ; } ;
>>
>> Here I got "event is not defined".
> 
> The proper approach is
>    var l = document.createElement("li");
>    l.onclick = function(evt) {
>      doMyFunction(evt.target);
>    };
> at least for Mozilla, Opera, Safari, Chrome and IE 9 (in IE 9 mode).
> For earlier IE versions you need
>    var l = document.createElement("li");
>    l.onclick = function(evt) {
>      var event = evt || window.event;
>      doMyFunction(event.target || event.srcElement);
>    };

If one uses the more error-prone boolean shortcut syntax instead of `typeof' 
testing, it should be

     l.onclick = function(evt) {
       var event = evt || window.event;
       if (event)
       {
         doMyFunction(event.target || event.srcElement);
       }
     };

as one should not assume that if `evt' type-converts to false, 
`window.event' must refer to an object.  However, that approach is 
proprietary, even though for historical reasons it is probably most 
compatible.

The standards-compliant approach is, and the proper approach has become

     l.addEventListener("click", function(evt) {
       doMyFunction(evt.target);
     }, false);

since IE/MSHTML 9 supports it in Standards Mode.  Until that remains less 
compatible (there is still IE/MSHTML < 9), using a wrapper method like 
jsx.dom.addEventListener() [1] is recommended, so as not to overwritethe 
primary event listener if one has been added, e. g., with the `onclick' 
attribute, and not to interfere with the order of event listeners of an 
element.


PointedEars
___________
[1] 
<http://pointedears.de/websvn/filedetails.php?repname=JSX&path=%2Ftrunk%2Fdom%2Fevents.js>
-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

<ul><li><ul><li>... onclick() only for one node ? Jörg Weule <weule@7b5.de> - 2011-11-25 16:10 +0100
  Re: <ul><li><ul><li>... onclick() only for one node ? Arno Welzel <usenet@arnowelzel.de> - 2011-11-25 17:30 +0100
  Re: <ul><li><ul><li>... onclick() only for one node ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-25 17:58 +0100
    Re: <ul><li><ul><li>... onclick() only for one node ? Jörg Weule <weule@7b5.de> - 2011-11-25 18:42 +0100
      Re: <ul><li><ul><li>... onclick() only for one node ? Martin Honnen <mahotrash@yahoo.de> - 2011-11-25 19:11 +0100
        Re: <ul><li><ul><li>... onclick() only for one node ? Jörg Weule <weule@7b5.de> - 2011-11-25 19:37 +0100
        Re: <ul><li><ul><li>... onclick() only for one node ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-25 20:04 +0100
  Re: <ul><li><ul><li>... onclick() only for one node ? "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2011-11-25 20:27 +0200
    Re: <ul><li><ul><li>... onclick() only for one node ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-25 20:28 +0100
      Re: <ul><li><ul><li>... onclick() only for one node ? "J.R." <groups_jr-1@yahoo.com.br> - 2011-11-25 21:57 -0200
        Re: <ul><li><ul><li>... onclick() only for one node ? "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2011-11-26 09:14 +0200
          Re: <ul><li><ul><li>... onclick() only for one node ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-26 12:53 +0100
        Re: <ul><li><ul><li>... onclick() only for one node ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-26 12:51 +0100

csiph-web