473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to stop <li> onclick bubble-up...?

I'd like to show tree structures using collapsible multi-level nested <ul>
lists (with open/closed "disclosure triangles" as list-style-images).
Something like this:

<ul>
<li onclick="alert('Level 1 clicked');">Level 1
<ul>
<li onclick="alert('Level 2 clicked');">Level 2
</li>
</ul>
</li>
</ul>

Clicking on the text following <li> (or on the list-style-image)
collapses/expands the rest of the content of that <li> (not shown here).

The problem is that each click bubbles up to the topmost list item. That is,
a click on "Level 2" first executes the innermost <li> onclick handler, then
the one for the next-outer-level <li> and so on. How can I make it so that
only the handler for the <li> that's clicked is executed? (returning false
from onclick didn't help in IE 6)

(I know I could wrap the text in <span>s and add the onclick handlers to
those instead, but that's a bit ugly, and the list-style-image would be out
of action)

Joakim Braun

Jul 23 '05 #1
5 14397
On Tue, 7 Dec 2004 20:37:26 +0100, Joakim Braun
<jo**********@jfbraun.removethis.com> wrote:

[snip]
The problem is that each click bubbles up to the topmost list item.
[...] How can I make it so that only the handler for the <li> that's
clicked is executed?
You should be able to stop propagation with

if(evt.stopPropagation) {evt.stopPropagation();}
evt.cancelBubble = true;

It's not going to work with NN4, though it should with IE4.

An alternative solution that would work in other situations (but not this
one) is to check the origin of the event. If it matched the current
element, you know you act on that event. If it didn't, ignore the event.
It is possible to see if the target is an immediate child, but I such a
solution would be more likely to work in more browsers than the one above
(I could be wrong, though).
(returning false from onclick didn't help in IE 6)


Returning false may cancel the action of that event, but it won't stop it
propagating.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
"Michael Winter" <M.******@blueyonder.co.invalid> skrev i meddelandet
news:opsinmw206x13kvk@atlantis...
On Tue, 7 Dec 2004 20:37:26 +0100, Joakim Braun
<jo**********@jfbraun.removethis.com> wrote:

[snip]
The problem is that each click bubbles up to the topmost list item.
[...] How can I make it so that only the handler for the <li> that's
clicked is executed?


You should be able to stop propagation with

if(evt.stopPropagation) {evt.stopPropagation();}
evt.cancelBubble = true;

It's not going to work with NN4, though it should with IE4.

<snip>

Thanks, that was helpful. Now to see what Opera says...

(tried tables before, but the non-nestability of <tbody>s (and consequent
use of subtables with multiple spacer columns for indentation) seemed too
hacky)

Joakim Braun
Jul 23 '05 #3
On Tue, 7 Dec 2004 21:52:49 +0100, Joakim Braun
<jo**********@jfbraun.removethis.com> wrote:
"Michael Winter" <M.******@blueyonder.co.invalid> skrev i meddelandet
news:opsinmw206x13kvk@atlantis...
[snip]
if(evt.stopPropagation) {evt.stopPropagation();}
evt.cancelBubble = true;


[snip]
Thanks, that was helpful. Now to see what Opera says...


Opera supports the DOM Events module, so you'll have no problems there.
Any recent DOM-conforming browser should execute the Event.stopPropagation
method. IE is the reason for the second line.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4
Joakim Braun wrote:
I'd like to show tree structures using collapsible multi-level nested <ul> lists (with open/closed "disclosure triangles" as list-style-images).
Something like this: <ul>
<li onclick="alert('Level 1 clicked');">Level 1
<ul>
<li onclick="alert('Level 2 clicked');">Level 2
</li>
</ul>
</li>
</ul> Clicking on the text following <li> (or on the list-style-image)
collapses/expands the rest of the content of that <li> (not shown

here).

(snip)

Could always use bubbling to your advantage, by processing click events
at a higher level.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>feh!</title>
<style type="text/css">

.there { display: block; }
.nowhere { display: none; }

</style>
<script type="text/javascript">
//<![CDATA[

function process(e)
{
e = e || (window.event || {});
var el, tgt = e.srcElement || e.target;
if (tgt && (el = tgt.getElementsByTagName('ul').item(0)))
el.className = 'therenowhere'.replace(el.className, '');
}

onload = function()
{
document.getElementsByTagName('body').item(0).oncl ick = process;
}

//]]>
</script>
</head>
<body>
<ul>
<li>Level 1
<ul class="nowhere">
<li>Level 2
<ul class="nowhere">
<li>Level 3
</li>
</li>
</ul>
</li>
</ul>
</body>
</html>

Barely a demo.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #5
"Rob B" <fe******@hotmail.com> skrev i meddelandet
news:1102455247.9542512efc4f61acd5d7362393ae8aed@t eranews...
<snip>
Could always use bubbling to your advantage, by processing click events
at a higher level.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>feh!</title>
<style type="text/css">

there { display: block; }
nowhere { display: none; }

</style>
<script type="text/javascript">
//<![CDATA[

function process(e)
{
e = e || (window.event || {});
var el, tgt = e.srcElement || e.target;
if (tgt && (el = tgt.getElementsByTagName('ul').item(0)))
el.className = 'therenowhere'.replace(el.className, '');
}

onload = function()
{
document.getElementsByTagName('body').item(0).oncl ick = process;
}

//]]>
</script>
</head>
<body>
<ul>
<li>Level 1
<ul class="nowhere">
<li>Level 2
<ul class="nowhere">
<li>Level 3
</li>
</li>
</ul>
</li>
</ul>
</body>
</html>


How cute - no JS handlers at all in the source code. I'll remember that one.

Joakim Braun
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Michael | last post by:
This is a two-part question to which I haven't been able to find an answer anywhere else. 1. Is it possible to format the bullet/number character of the <li>? In my styles sheet, I have the <li>...
1
by: Randall Sell | last post by:
OK, I am utterly stumped. The code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <style type="text/css"> ul {...
4
by: Matt | last post by:
Hi, Got an unordered list with 100% width, with 5 list items of 20% width styled to fill the width of the container element. Renders fine in Mozilla, but when you change the size of the window...
4
by: Mark | last post by:
Hopefully I 'm missing something silly, but I can't find an easy way to loop all list items in a simple <ol>. I was hoping a for loop as shown below would be enough, however clicking "alert all" in...
4
by: abs | last post by:
Anybody has an idea how to get the <ul> element which is not nested in <li> element ? In other words I have several lists like this: <ul id="1"> <li>Aaaaaaaa</li> <li>Bbbbbbbb</li>...
2
by: Shaun | last post by:
Hello! I have a quick question regarding CSS and having it applied to all elements. I am trying to eliminate the gap between a paragraph and a list that usually occurs in html and I've found...
2
by: Andrew Donaldson | last post by:
I'd welcome some help in understanding what's going on with graphical browsers in the navigation list at: http://www.bounceandtickle.org.uk/index.html (This site is not about what it might...
6
by: cbartlett | last post by:
If I have: <ul id="theUL"> <li id="anLi1">some text</li> <li id="anLi2">some other text</li> <ul> How can I get the value of the text within the the li's? Any help much appreciated. thanks...
2
by: celtique | last post by:
Hello everybody! I've just registered to this forum and yet got a question. :) I've got some database, that is processed by PHP in a 'while' loop and each element that the loop draws (<li>) gets...
5
by: Syl | last post by:
Hello experts!! The top menu navigation bar displays perfectly in IE, but does not display properly in Mozilla or Netscape : http://checkeredshirt.com/textonly.html For some reason the non-IE...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.