473,439 Members | 1,920 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,439 software developers and data experts.

DOM children & events

Hi,

I'm having some difficulties with a menu I'm making. I build up the
menu through DOM. I append childnodes to a tree. 2 types of children
are possible: url (a hyperlink) and sub (a submap).

The tree is thus of a structure

main
|-url1
|-url2
|-submap
|--url21
|--url22

To open up the submaps I set the onclick event to a function I
created:

itemnode.onclick=function() { openMap(itemnode) }

The problem now is that 1 click in a submap results in several times
the openmap function. A click on the url21 node is in fact a click on
a child of submap so the function in the onclick event of this submap
is called. But it is also a click on a child of main, so this onclick
function is also called.

How can you limit that the onclick event handler is only called on the
1 node (ic. submap) where you actually clicked on submap instead of
its urls?

Full code listing below.

Thanks in advance

Rick
---------------------------------

menu.js

/* file: <title>menu.js</title> */
var content = [];
var this_menu;
show_menu();
function show_menu(){
init();
}
function init(){
var format = TREE_NODES.format;
var top = format.top;
var left = format.left;
var width = format.width;
var height = format.height;
var background_color = format.background_color;
var node = document.getElementsByTagName("body").item(0);
var root = document.createElement('div');
root.style.position = "absolute";
root.style.top = top;
root.style.left = left;
root.style.width = width;
root.style.height = height;
root.style.backgroundColor = background_color;
node.appendChild(root);
content = TREE_NODES.sub;
for (var i=0; i < content.length; i++){
var contentNode = add(content[i]);
root.appendChild(contentNode);
}
}
function add( item ){
var html = (item.html)?item.html:0;
var url = (item.url)?item.url:0;
var sub = (item.sub)?item.sub:0;
var itemnode;
itemnode = document.createElement('div');
itemnode.style.position = "relative";
itemnode.style.left = 10;

if (url){
var pic = document.createElement('img');
pic.setAttribute("src", "page.gif");
itemnode.appendChild(pic);

var urlnode = document.createElement('a');
urlnode.setAttribute("target", "main");
itemnode.appendChild(urlnode);
}
if (html){
var textNode = document.createTextNode(html)

if (sub) {
var pic = document.createElement('img');
pic.setAttribute("src", "open.gif");
itemnode.appendChild(pic);
}
if (urlnode) {
urlnode.appendChild(textNode);
}
else {

var mapnode = document.createElement('a');
itemnode.appendChild(mapnode);
itemnode.onclick=function() { openMap(itemnode) }
mapnode.appendChild(textNode);
}
if (url) urlnode.setAttribute('href',url);
// if (url)
// itemnode.onclick=function() { alert("test"); }

}
if (sub){
for (var j=0; j < sub.length; j++){
var dit = sub[j];
var subNode = add(dit);
itemnode.appendChild(subNode);
}
}
return itemnode;
}

function openMap(node){
alert("openMap");

// alert(node.childNodes.item(0).nodeName); // IMG of the map
// alert(node.childNodes.item(1).nodeName); // A of the map

try {
for(i=2 ; i < node.childNodes.length ; i++) {
if (node.childNodes.item(i).style.display == "none" ) {
node.childNodes.item(0).src = "open.gif";
node.childNodes.item(i).style.display="block";
}
else {
node.childNodes.item(0).src = "closed.gif";
node.childNodes.item(i).style.display="none";
}
}
}
catch (ex) {
alert("error: "+ex);
}
}

-------------------------------------

structure.js
/*
file: <title>structure.js</title>
This file contains the menustructure to be displayed with node
children.
*/
var TREE_NODES={
format:{
left: 10,
top: 10,
width: 200,
height: 300,
o_image: "open.gif",
c_image: "closed.gif",
p_image: "page.gif",
background_color: "#ffffff",
main_background_color: "#ffffff",
animation:1,
padding:2,
dont_resize_back:1
},
sub:
[ {html: 'x-html',
sub:[
{html:'w3c', url:'http://www.w3.org/TR/xhtml2/'},
{html:'dom', url:'http://www.w3.org/DOM/'},
{html:'submap',
sub:[
{html:'test1', url:'http://www.w3.org/DOM/'},
{html:'test2', url:'http://www.w3.org/DOM/'},
{html:'submap2',
sub:[
{html:'test21', url:'http://www.w3.org/DOM/'}
]}
]}]
},
{html: 'xml',
sub:[
{html:'xml', url:'http://www.w3.org/XML/'},
{html:'xslt',
sub:[
{html:'main page', url:'http://www.w3.org/Style/XSL/'},
{html:'tutorial', url:'http://www.renderx.com/tutorial.html'}
]}]}]}
Jul 23 '05 #1
2 2214


DaRik wrote:
I'm having some difficulties with a menu I'm making. I build up the
menu through DOM. I append childnodes to a tree. 2 types of children
are possible: url (a hyperlink) and sub (a submap).

The tree is thus of a structure

main
|-url1
|-url2
|-submap
|--url21
|--url22

To open up the submaps I set the onclick event to a function I
created:

itemnode.onclick=function() { openMap(itemnode) }

The problem now is that 1 click in a submap results in several times
the openmap function. A click on the url21 node is in fact a click on
a child of submap so the function in the onclick event of this submap
is called. But it is also a click on a child of main, so this onclick
function is also called.

How can you limit that the onclick event handler is only called on the
1 node (ic. submap) where you actually clicked on submap instead of
its urls?


Events bubble up the tree from child nodes to parent nodes. DOM Level 2
as implemented in Mozilla, Netscape 6/7, Opera 7 allows you to stop the
propagation of an event e.g.
itemnode.onclick = function (evt) {
openMap(itemnode);
if (evt.stopPropagation) {
evt.stopPropagation();
}
}
IE doesn't support that but has a property called cancelBubble that you
can set to true:
itemnode.onclick = function (evt) {
openMap(itemnode);
if (evt.stopPropagation) {
evt.stopPropagation();
}
if (window.event) {
window.event.cancelBubble = true;
}

Alternatively you could only assign an event handler to the parent
element and then check the event.srcElement/evt.target in your code.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #2
The problem you are describing is the result of "event bubbling". Event
bubbling allows parent elements to respond to events fired by their
desendents.

In your case your onclick routine needs to be slightly more intelligent and
only process the node when necessary. Here is the processing logic I would
implement:

1. get a reference to the source element (the element that fired the event)
2. if the source element is not an A element it must be a subnode so
open/close the subnode
3. cancel the bubble
4. exit

for IE the code would look like

function openMap(node){

if (event.srcElement.tagName != "A"){

try {
for(i=2 ; i < node.childNodes.length ; i++) {
if (node.childNodes.item(i).style.display == "none" ) {
node.childNodes.item(0).src = "open.gif";
node.childNodes.item(i).style.display="block";
}
else {
node.childNodes.item(0).src = "closed.gif";
node.childNodes.item(i).style.display="none";
}
}
catch (ex) {
alert("error: "+ex);
}
}

event.cancelBubble = true;
return;

}
Jul 23 '05 #3

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

Similar topics

3
by: wenke | last post by:
Hi, I am using the following code (see below) from php.net (http://www.php.net/manual/en/ref.xml.php, example 1) to parse an XML file (encoded in UTF-8). I changed the code slightly so that the...
6
by: Luke Dalessandro | last post by:
I'm not sure if this is the correct forum for platform specific (Mozilla/Firefox) javascript problems, so just shout and point me to the correct newsgroup if I'm being bad. Here's the deal... ...
0
by: jphelan | last post by:
I have a subform that works fine until you import it into a new database when it crashes if you try to open it in either disign or form view. The form, "Attendees_Subform" in my application was...
3
by: Trevor | last post by:
Hello, How can I setup an event in C#? I would like for class A to have an event, and class B to intercept the event. Class B should not be able to call the event itself, only class A. How is...
175
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be...
6
by: Barry Gast | last post by:
Hi. I have an MDI Parent form with multiple child windows. When I close the parent form, the Closing events of the children forms are not executing. Do I have to close all the child forms in the...
1
by: Marius Groenendijk | last post by:
Dear Group, I have a MDI with children which may be editing data. If a MDI child closes its Closing handler asks a question 'exit w/out saving?'. On closing the MDI its pops a question 'Exit...
10
by: qwertycat | last post by:
I'm new to multi-process programming, should one avoid forking children from children of a parent? I'd like to spawn 10 children from the parent and each of those children spawns another 5...
4
by: RgeeK | last post by:
From my newbie perspective I understand that Javascript is a single threaded environment and so interrupt-driven events are going to be somewhat challenging. But, I have an issue which seems to...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
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,...
0
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...
1
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.