R
Rasmus Kromann-Larsen
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;
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
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.
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;
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
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.