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

Event bubbling...

Hey,

Messing with JavaScript on a webpage, I ran into a small problem.
Wanting to have a table cell select it's contained radiobutton in a
rather non-specific way, I wrote the following javascript:
function click_it(element)
{
var x = element.childNodes

for(var i=0;i<x.length;x++)
{
var tag = x[i];
if(!tag.tagName || tag.tagName != "INPUT")
continue;

tag.checked = !tag.checked;
}
}
It works fine for clicking the table cell and selecting contained
radiobuttons. However, when actually clicking the radiobutton, the
browser decides (correctly) to first fire the internal radiobutton
click event, thus selecting the radiobutton - and THEN fireing the
click handler for my table cell, thus de-selecting it again :-P If
this was to only work for radiobuttons, I could easily make it do
"tag.checked = 1" every time, but it needs to work for checkboxes too,
so the de-select should work too (if checked).

I also realize I'd have problems with other "input" elements, but that
isn't related to this problem :-)

I figured it'd be a rather typical problem, but didn't find a solution
in my searches for it, so I decided to post here.
Thanks in advance,
- Rasmus.

Mar 27 '07 #1
2 1756
On Mar 27, 5:07 pm, "Rasmus Kromann-Larsen" <rasmu...@gmail.com>
wrote:
Hey,

Messing with JavaScript on a webpage, I ran into a small problem.
Wanting to have a table cell select it's contained radiobutton in a
rather non-specific way, I wrote the following javascript:

function click_it(element)
{
var x = element.childNodes

for(var i=0;i<x.length;x++)
{
var tag = x[i];
if(!tag.tagName || tag.tagName != "INPUT")
continue;

tag.checked = !tag.checked;
}

}

It works fine for clicking the table cell and selecting contained
radiobuttons. However, when actually clicking the radiobutton, the
browser decides (correctly) to first fire the internal radiobutton
click event, thus selecting the radiobutton - and THEN fireing the
click handler for my table cell, thus de-selecting it again
You have two options:

1. Add an onclick to the radio button to stop the event bubbling up,
but that doubles the number of onclick functions.

2. Use the event object passed to your click_it function to see if the
click came from a radio button. If so, do nothing. If not, use your
current function to toggle the checked property.
--
Rob

Mar 27 '07 #2
On Mar 27, 3:17 pm, "RobG" <r...@iinet.net.auwrote:
On Mar 27, 5:07 pm, "Rasmus Kromann-Larsen" <rasmu...@gmail.com>
wrote:
Hey,
Messing with JavaScript on a webpage, I ran into a small problem.
Wanting to have a table cell select it's contained radiobutton in a
rather non-specific way, I wrote the following javascript:
function click_it(element)
{
var x = element.childNodes
for(var i=0;i<x.length;x++)
{
var tag = x[i];
if(!tag.tagName || tag.tagName != "INPUT")
continue;
tag.checked = !tag.checked;
}
}
It works fine for clicking the table cell and selecting contained
radiobuttons. However, when actually clicking the radiobutton, the
browser decides (correctly) to first fire the internal radiobutton
click event, thus selecting the radiobutton - and THEN fireing the
click handler for my table cell, thus de-selecting it again

You have two options:

1. Add an onclick to the radio button to stop the event bubbling up,
but that doubles the number of onclick functions.

2. Use the event object passed to your click_it function to see if the
click came from a radio button. If so, do nothing. If not, use your
current function to toggle the checked property.

--
Rob
Thanks for the reply Rob. I went with solution 1 :-) I tried with
solution 2, but it didn't readily work for me, so I just went back to
what I'd figured out.

Thanks,
- Rasmus.

Mar 28 '07 #3

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

Similar topics

5
by: Mark Szlazak | last post by:
Apparently there is a textarea onscroll event bubbling bug in Firefox and Mozilla: https://bugzilla.mozilla.org/show_bug.cgi?id=229089 I'm trying to check for this bug with the following...
4
by: Wee Bubba | last post by:
the following event handler checks to see if any parent control has attached to the event 'SearchItemSelect'. i use it for event bubbling whenever one of my search items has been selected and at...
7
by: Girish | last post by:
OK.. phew. Playing with data grids for the past few days has been fun and a huge learning experience.. My problem. I have a requirement to display a gird with a gird. Within the embedded grid,...
3
by: Nathan Sokalski | last post by:
I have three LinkButtons in the HeaderTemplate of my DataList (I use them to let the user determine what to sort the list by). I am assuming that the event will be bubbled to the ItemCommand event...
7
by: comzy | last post by:
I have created an event bubbling for my pager control which is used for implementing paging in data grid. Althoug it worked very fine in .NET 1.1 it is throwing the following error after i migrated...
0
by: Peter Rilling | last post by:
Hi. Okay, I have read only other news postings that winforms to not support the concept of "event bubbling" that web apps do. Nevertheless, I need to implement something such that if I have a...
4
by: Nathan Sokalski | last post by:
I have a DataList that has an Button as one of the controls in it's ItemTemplate. The Button has a CommandName="delete" attribute, but when I click it the DeleteCommand event doesn't even get...
17
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"); }
1
by: David | last post by:
Hi all, I have a page that inherits from my own page class that inherits from system.web.ui.page. In my page, I have a button click event that saves content of the page. The saving is...
2
by: alhalayqa | last post by:
hi, in MSIE, you can attach onclick event to both anchor and document objects. here is some code : document.onclick = function() {alert('document clicked ...'); } <a href= " " ...
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: 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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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
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,...

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.