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

Radio Event Not Registered

MC
Question,
Why does the following code register the event for input type button but not
radio? (And how do I make this happen?)
Thanks,
MC

<HTML>
<FORM name='x' action=''>
<input type='radio' name='rbTest' id='YES_1' value='1'>YES
<input type='radio' name='rbTest' id='NO_1' value='0'>NO
<input type='button' name='btnTest' id='btnTestid' value='test'>
</FORM>
<SCRIPT LANGUAGE="Javascript" type="text/javascript">
document.x.btnTest.onclick = doTest;
document.x.rbTest.onclick = doTest;
function doTest(e) {
alert('doTest');
}
</SCRIPT>
</HTML>
Jul 22 '08 #1
3 1296
On Jul 22, 3:51 pm, MC wrote:
Question,
Why does the following code register the event for input
type button but not radio? (And how do I make this happen?)
Thanks,
MC

<HTML>
<FORM name='x' action=''>
<input type='radio' name='rbTest' id='YES_1' value='1'>YES
<input type='radio' name='rbTest' id='NO_1' value='0'>NO
<input type='button' name='btnTest' id='btnTestid' value='test'>
</FORM>
<SCRIPT LANGUAGE="Javascript" type="text/javascript">
document.x.btnTest.onclick = doTest;
document.x.rbTest.onclick = doTest;
<snip>
If you have multiple like-named form controls (usually radio buttons)
then referencing them by name results in a collection of all of those
like-named controls. Setting an - onclick - property on a collection
object is probably harmless but is also not useful.

You would need to loop through the collection and assign the - onclick
- listener to each form control element in the collection in tern.
Jul 22 '08 #2
MC
Henry,
Thank you for pointing this out. Adding the following resolved the issue.
MC

function addRadioEvent(e) {
for (var i=0; i < e.length; i++) {
e[i].onclick = myEventClick;
}
}
Jul 22 '08 #3
Henry wrote:
On Jul 22, 3:51 pm, MC wrote:
>Question,
Why does the following code register the event for input
type button but not radio? (And how do I make this happen?)
[...]
<HTML>
<FORM name='x' action=''>
<input type='radio' name='rbTest' id='YES_1' value='1'>YES
<input type='radio' name='rbTest' id='NO_1' value='0'>NO
<input type='button' name='btnTest' id='btnTestid' value='test'>
</FORM>
<SCRIPT LANGUAGE="Javascript" type="text/javascript">
document.x.btnTest.onclick = doTest;
document.x.rbTest.onclick = doTest;
This is not Valid markup and so is not supposed to work in the first place.
<snip>
If you have multiple like-named form controls (usually radio buttons)
then referencing them by name results in a collection of all of those
like-named controls. Setting an - onclick - property on a collection
object is probably harmless but is also not useful.

You would need to loop through the collection and assign the - onclick
- listener to each form control element in the collection in tern.
It suffices and is much more efficient to assign the `click' event listener
to the `form' element object or another common ancestor element object
instead, because the `click' event bubbles in all known DOMs:

function doTest()
{
// ...
}

and then

<form ... onclick="doTest()">
...
</form>

or

document.forms["x"].addEventListener("click", doTest, false);

or

document.forms["x"].onclick = doTest;

Note that the third approach is proprietary (for `onclick'). In contrast
to the second, standards-compliant approach, it requires some work if a
potential primary event listener is not to be replaced.

As obvious by the first approach, the `form' element does not need a name.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Jul 23 '08 #4

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

Similar topics

2
by: Matt | last post by:
I have radio buttons on a form, and when the user makes the selection, it will trigger the event and send the form to the web server and print the selected value. In Case 1, I just use submit...
6
by: amith | last post by:
hi, i have some 10 radio buttons meant to take the rating from the user. ex: 1 2 3 4 5 6 7 8 9 10 looks O O O O O O O O O O 1 2 3 4 5 6 7 8 9 10 features O O O O O O O O O O
6
by: HD | last post by:
Hi. I have an asp page with radio buttons and a combobox... when the user clicks a radio button, I want the form to submit so I can execute the ASP code in order to change the list shown in the...
2
by: NishSF | last post by:
Would anyone have any suggestions/javascript code so that if one clicks the Radio Button "Yes" below he has the option of selecting any of the six CheckBox below. If the user clicks on Radio Button...
1
by: Jerry | last post by:
We have a 10-question quiz for kids, each question being a yes or no answer using radio selections. I'd like to keep a current total of yes's and no's at the bottom of the quiz (if the user selects...
1
by: Joe Attardi | last post by:
Hi all, On a form on one of my pages I have two <select> elements, and each one is paired up with a radio button. The idea is to choose an item from one list or the other and select the radio...
3
w33nie
by: w33nie | last post by:
I want to disable the text boxes, captain_name and captain_email, but only if the radio button, captain_guarantee, has NOT been checked. how do i do this? <form name="formTeamApplication"...
4
by: Z.K. | last post by:
I started a forms application and I put on the form three buttons. In the functions for the radio buttons I put in a single messagebox like: MessageBox("Hello 1") MessageBox("Hello 2") ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...

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.