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


Groups > comp.lang.javascript > #8636

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

Message-ID <1834269.dtTrMRavdF@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2011-11-25 17:58 +0100
Subject Re: <ul><li><ul><li>... onclick() only for one node ?
Newsgroups comp.lang.javascript
References <9j9pfhFe3lU1@mid.individual.net>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


Jörg Weule wrote:

> at <ul><li><ul><li>...</li></ul></li></ul> I got the click event on the
> whole tree and want to process the event for only on one node.
> 
> How can i stop the event processing calling my function for any node at
> the hirachie?

If this is really only for one node, you can call the stopPropagation() 
method (standards-compliant) or set the event object's `cancelBubble' 
property to `true' (MSHTML) in the event listener.  However, the drawback of 
this is that *no* element "upwards" in the tree will receive that event 
then, which may not be wanted.

If the latter is important, or if this instead for multiple nodes at the 
same nesting level, you should not do this.  For then you need to stop event 
progagation at every child node.  This comparably inefficient approach is 
propagated by, e. g., jQuery and other selector-based libraries, where you 
would first select elements by a criterion (usually a `class' attribute 
value) and then add an event listener to each matching element.

It is therefore better (for bubbling events like `click') to add only one 
event listener to an ancestor element (here: the `ul' element) in which you 
compare the event target against the object the event has bubbled up to 
(e.target == this, or e.srcElement == this in MSHTML [do not use 
attachEvent()]), and only perform the action for relevant event targets.  In 
case of remaining ambiguity, you can use e. g. the event target's `class' 
attribute value as well.  Be aware that text nodes can be event targets, 
too.


PointedEars
-- 
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