473,774 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

onClick / Nested Divs

Can someone please explain how onclick works with nested, absolutely
positioned div containers? I would expect, when several divs are on
top of one another, clicking on them would trigger an onclick event
for each div. But it's not working that way. Take a look at the
attached file. Clicking in the center of the "orange" div, I would
think, would trigger an alert for all five of the divs. In fact, it
doesn't even trigger one for the "orange" div.

=============== ======

<html><head>
<script type="text/javascript">
function show(id){ alert( "clicked on " + id); }
</script>
<style type="text/css">
#gray {position:relat ive;height:300p x;width:300px;m argin-
right:auto;marg in-left:auto;borde r:thin solid #666;z-index:10}
#blue {position:absol ute;top:20px;le ft:20px;height: 100px;width:
100px;margin-right:auto;marg in-left:auto;borde r:thin solid blue;z-
index:20}
#red {position:absol ute;top:0;left: 0;height:100px; width:100px;mar gin-
right:auto;marg in-left:auto;borde r:thin solid red;z-index:30}
#green {position:absol ute;top:5px;lef t:5px;height:30 px;width:
30px;margin-right:auto;marg in-left:auto;borde r:thin solid green;z-
index:40}
#orange {position:absol ute;top:10px;le ft:10px;height: 30px;width:
30px;margin-right:auto;marg in-left:auto;borde r:thin solid orange;z-
index:50}
</style>
</head><body>
<div id="gray" onclick="show(' gray')">
<div id="blue" onclick="show(' blue')">
<div id="green" onclick="show(' green')"></div>
<div id="orange" onclick="show(' orange')"></div>
</div>
<div id="red" onclick="show(' red')"></div>
</div>
</body></html>
Jun 27 '08 #1
2 3018
pr
pr wrote:
Mike wrote:
>Can someone please explain how onclick works with nested, absolutely
positioned div containers? I would expect, when several divs are on
top of one another, clicking on them would trigger an onclick event
for each div. But it's not working that way. Take a look at the
attached file. Clicking in the center of the "orange" div, I would
think, would trigger an alert for all five of the divs. In fact, it
doesn't even trigger one for the "orange" div.
[...]
If no div contained any other div, you would get the results I guess you
anticipate.
PS: re-reading your post, I see you anticipate something else. To have a
click event bubble through all the divs you would need to structure your
HTML so:

<div id="gray" onclick="show(' gray')">
<div id="blue" onclick="show(' blue')">
<div id="green" onclick="show(' green')">
<div id="orange" onclick="show(' orange')">
<div id="red" onclick="show(' red')"></div>
</div>
</div>
</div>
</div>

CSS positioning has no effect on the manner in which events bubble, but
it may, as I have shown, prevent the initial element receiving the event.
Jun 27 '08 #2
Thanks!

The application I'm working on, actually has over 100 div containers,
so nesting them all inside one another seems impractical maybe even
problematic. After reading through your thoughts and playing around a
bit more, I think a better approach for my needs will be an image map
to trigger the events.

Thanks again, it was a great help!

Mike
PS: re-reading your post, I see you anticipate something else. To have a
click event bubble through all the divs you would need to structure your
HTML so:
CSS positioning has no effect on the manner in which events bubble, but
it may, as I have shown, prevent the initial element receiving the event.
Jun 27 '08 #3

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

Similar topics

9
4076
by: Dustin | last post by:
Here's what I am trying to do: (Names represent CSS classes.) I want to create a photo gallery. I want the entire gallery to be surrounded by a box named PhotoGallery. I want a fluid placement of the individual Items within PhotoGallery. An Item is a box that has two child boxes, PhotoBox and Caption. Photobox has an explicit width and height. PhotoBox contains an image whose dimensions may vary, but are constrained to be within...
3
5012
by: Philip | last post by:
I am trying to make a bunched of left-floated divs that will be contained in a larger div. the floated divs all contain a left-floated img and text of varying sizes. If I don't set a height (or height:auto) the larger div doesn't seem to see the nested divs, and they spill out the bottom. IE seems to automatically resize the larger div as long as I set the height to 100% or any pixel size. Mozilla doesn't recognize the percent, and...
2
1953
by: Guadala Harry | last post by:
Please follow my thinking here and tell me if I'm nuts or if this is a reasonable thing to do given the objective: I'd like to present 3 data values on an aspx page - each styled uniquely, and the total width to be limited to some arbitrary value (400px or so). Given that I'd like to limit the total width, I can't use a <td> because the width specified for a <td> specifies a minimum and not a maximum. Consequently I'll use a DIV...
4
3651
by: Dylan Parry | last post by:
Hi, I'm not sure what the correct term for this is, but I want to have two objects (say HTML divs) positioned absolutely one on top of the other, with the lower of the two having an onclick event attached to it. Now as you can guess, if I then click on where the top and bottom object overlap, the top object intercepts the click and the onclick event for the bottom object is never fired.
17
10219
by: dan_williams | last post by:
I have the following test web page:- <html> <head><title>Test</title> <script language="Javascript"> <!-- function fnTR() { alert("TR"); }
4
1479
by: D | last post by:
Hi, I've been toying around with master pages and am trying to set up your standard banner across the top and 3 areas below that, left content, center content and right content. I have the banner part being the top level master page and the left content be nested master page named subMaster.master. In this I have 3 div's that I've named
2
2182
by: Ben C. | last post by:
Hi If I have a container div and inside the container div I float two divs (one left and one right), the containting div will collapse as the floated divs will no longer prop it up, meaning that any background colour or image on the containing div will not be visible. Now, suppose I put left and right padding on the containing div. How come, if the containing div has collapsed, does the padding of the container still apply to the two...
1
4310
rahulephp
by: rahulephp | last post by:
CSS Tabs - contents of a div disappear in IE & onclick is not working Here is the code: Swaps the "active" tab and "inactive" tab and swaps out the new content via the display style attribute: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
0
9621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10040
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.