473,778 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reusable function to get radio button's selected value?

According to
http://www.quirksmode.org/js/forms.html
you need to look through a radio button's members to find the value:
>Radio buttons
Unfortunatel y it's not possible to see at once which radio button in a group the user has checked. You need to go >through all radio's and see which one's checked property is true.
I tried writing a reusable function to return the value, see code at:
http://www.geocities.com/usenet_daug...ript_radio.htm

but for some reason I can only get a value if I hard code the control
name.

Can anyone explain how to do this?

Thanks
Jun 27 '08 #1
5 3601
On Apr 21, 12:21*pm, mad.scientist.. .@gmail.com wrote:
According to
* *http://www.quirksmode.org/js/forms.html
you need to look through a radio button's members to find the value:
Radio buttons
Unfortunately it's not possible to see at once which radio button in a group the user has checked. You need to go >through all radio's and see which one's checked property is true.

I tried writing a reusable function to return the value, see code at:
* *http://www.geocities.com/usenet_daug...ript_radio.htm

but for some reason I can only get a value if I hard code the control
name.

Can anyone explain how to do this?

Thanks
Does this work for you?

function getSelectedValu eForName(name) {
for (var i = 0; i < document.getEle mentsByTagName( "input").length ; i+
+) {
var input = document.getEle mentsByTagName( "input")[i];
if (input.type == "radio" && input.checked) {
return input.value;
}
}
return null;
}

You would call getSelectedValu eForName('Radio 1'); where your radio
buttons share the name Radio1.

HTH.
Jun 27 '08 #2
Tom Cole wrote:
On Apr 21, 12:21�pm, mad.scientist.. .@gmail.com wrote:
According to
� �http://www.quirksmode.org/js/forms.html
you need to look through a radio button's members to find the value:
>Radio buttons
>Unfortunatel y it's not possible to see at once which radio button in a group the user has checked. You need to go >through all radio's and see which one's checked property is true.
I tried writing a reusable function to return the value, see code at:
� �http://www.geocities.com/usenet_daug...ript_radio.htm

but for some reason I can only get a value if I hard code the control
name.

Can anyone explain how to do this?

Thanks

Does this work for you?

function getSelectedValu eForName(name) {
for (var i = 0; i < document.getEle mentsByTagName( "input").length ; i+
+) {
var input = document.getEle mentsByTagName( "input")[i];
if (input.type == "radio" && input.checked) {
return input.value;
}
}
return null;
}

You would call getSelectedValu eForName('Radio 1'); where your radio
buttons share the name Radio1.

HTH.
Tim, HTH,
Your solution was good, but from performance point of view, you
can do better.
Caching your "input" collection will greatly improve speed. Also, for
loops will calculate
the collection's length for each iteration.

function getSelectedValu eForName(name) {
var inputs = document.getEle mentsByTagName( "input");
for (var i = 0, len = inputs.length; i < len; i++) {
if (inputs[i].type == "radio" && input[i].checked) {
return input[i].value;
}
}
return null;
}

Even better, if you use Prototype JS Library.

var checked_values = $$
('input:checked[type="radio"]').pluck('value ');

-- Peroli Sivaprakasam
Jun 27 '08 #3
On Apr 22, 10:16Â*am, Peroli <per...@gmail.c omwrote:
Tom Cole wrote:
On Apr 21, 12:21�pm, mad.scientist.. .@gmail.com wrote:
According to
� �http://www.quirksmode.org/js/forms.html
you need to look through a radio button's members to find the value:
Radio buttons
Unfortunately it's not possible to see at once which radio button in a group the user has checked. You need to go >through all radio's and see which one's checked property is true.
I tried writing a reusable function to return the value, see code at:
� �http://www.geocities.com/usenet_daug...ript_radio.htm
but for some reason I can only get a value if I hard code the control
name.
Can anyone explain how to do this?
Thanks
Does this work for you?
function getSelectedValu eForName(name) {
Â* Â*for (var i = 0; i < document.getEle mentsByTagName( "input").length ; i+
+) {
Â* Â* Â* Â* Â* Â*var input = document.getEle mentsByTagName( "input")[i];
Â* Â* Â* Â* Â* Â*if (input.type == "radio" && input.checked) {
Â* Â* Â* Â* Â* Â* Â* Â* Â* Â*return input.value;
Â* Â* Â* Â* Â* Â*}
Â* Â*}
Â* Â*return null;
}
You would call getSelectedValu eForName('Radio 1'); where your radio
buttons share the name Radio1.
HTH.

Tim, HTH,
Â* Â* Your solution was good, but from performance point of view, you
can do better.
Caching your "input" collection will greatly improve speed. Also, for
loops will calculate
the collection's length for each iteration.

function getSelectedValu eForName(name) {
Â* Â* Â* Â* var inputs = document.getEle mentsByTagName( "input");
Â* Â* Â* Â* for (var i = 0, len = inputs.length; i < len; i++) {
Â* Â* Â* Â* Â* Â* Â* Â* if (inputs[i].type== "radio" && input[i].checked) {
s/b if (inputs[i].type == "radio" && inputs[i].checked) {
Â* Â* Â* Â* Â* Â* Â* Â* Â* Â* Â* Â* return input[i].value;
s/b return inputs[i].value;
Â* Â* Â* Â* Â* Â* Â* Â* }
Â* Â* Â* Â* }
Â* Â* Â* Â* return null;

}
I'm not convinced that using inputs[i] three times is more efficient
than a single assignment of var input =
document.getEle mentsByTagName( 'input')[i]. Maybe someone with better
understanding of what goes on behind the scenes could elaborate. I do
see your point though of not calling .length for each iteration.
Even better, if you use Prototype JS Library.

Â* Â*var checked_values = $$
('input:checked[type="radio"]').pluck('value ');

-- Peroli Sivaprakasam- Hide quoted text -

- Show quoted text -
Better for whom? Fewer lines of code in the OP's code? Maybe, but it
doesn't mean it'll perform better or be more readable code. What does
the $$ and pluck methods in Prototype look like?

Jun 27 '08 #4
Peroli wrote on 22 apr 2008 in comp.lang.javas cript:
function getSelectedValu eForName(name) {
var inputs = document.getEle mentsByTagName( "input");
for (var i = 0, len = inputs.length; i < len; i++) {
if (inputs[i].type == "radio" && input[i].checked) {
return input[i].value;
}
}
return null;
}
Where is the parameter called name used?

If the inputs have a common name in a form do:

var inputs = document.forms['myForm'].elements[name];

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 27 '08 #5
On Apr 22, 10:22 pm, "Evertjan." <exjxw.hannivo. ..@interxnl.net >
wrote:
Peroli wrote on 22 apr 2008 in comp.lang.javas cript:
function getSelectedValu eForName(name) {
var inputs = document.getEle mentsByTagName( "input");
for (var i = 0, len = inputs.length; i < len; i++) {
if (inputs[i].type == "radio" && input[i].checked) {
return input[i].value;
}
}
return null;
}

Where is the parameter called name used?

If the inputs have a common name in a form do:

var inputs = document.forms['myForm'].elements[name];

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Evertjan,
I agree. I will test my code before posting from now on.

Tim,
I agree to your first point that using inputs[i] 3 times is not
efficient.
In-fact, my test results showed that its slower by 10 - 30%.
I wrote a small test script and found that $$ is indeed faster in
Firefox and Safari.
Haven't tested it in IE, but I know it will be slower, because $$
uses native xpath.
Evaluate my test script and share your suggestions. I am open to
criticism.

--- Script (uses prototype.js) ---

<html>
<head>
<title>Benchmar k</title>
</head>
<body>
<form id="test_form" >
<input type="button" id="getValues" value="getValue s">
</form>
<span id="result" />
<script type="text/javascript" src="http://prototypejs.org/assets/
2008/1/25/prototype-1.6.0.2.js"></script>
<script type="text/javascript">
function getSelectedValu eForName(name) {
for (var i = 0; i <
document.getEle mentsByTagName( name).length; i++) {
var input = document.getEle mentsByTagName( name)[i];
if (input.type == "radio" && input.checked) {
return input.value;
}
}
return null;
}

function getSelectedValu eForName1(name) {
var inputs = document.getEle mentsByTagName( name);
for (var i = 0, len = inputs.length; i < len; i++) {
if (inputs[i].type == "radio" && inputs[i].checked) {
return inputs[i].value;
}
}
return null;
}

function getSelectedValu eForName2(name) {
var inputs = document.getEle mentsByTagName( name);
for (var i = 0, len = inputs.length; i < len; i++) {
var input = inputs[i];
if (input.type == "radio" && input.checked) {
return input.value;
}
}
return null;
}

function getSelectedRadi oValue() {
return $$('input:check ed[type="radio"]').pluck('value ');
}

function benchmark() {
var d = new Date();
(100).times(
function() {
var vals = getSelectedValu eForName('input ');
}
);
var t1 = (new Date()) - d;
d = new Date();
(100).times(
function() {
var vals = getSelectedValu eForName1('inpu t');
}
);
var t2 = (new Date()) - d;
d = new Date();
(100).times(
function() {
var vals = getSelectedValu eForName2('inpu t');
}
);
var t3 = (new Date()) - d;
d = new Date();
(100).times(
function() {
var vals = getSelectedRadi oValue();
}
);
var t4 = (new Date()) - d;
$('result').upd ate( "Tim's way:" + t1 + '<br />caching1:' + t2 +
'<br />caching2:' + t3 + '<br /$$:' + t4);
}

function init() {
var frm = $('test_form');
(1000).times(
function (count) {
frm.insert({bot tom:'<input type="radio" name="radio' +
count + '" value="value' + count + '" />'});
}
);

Event.observe(' getValues', 'click', benchmark);
}
Event.observe(w indow, 'load', init);
</script>
</body>
</html>
-- Peroli Sivaprakasam
Jun 27 '08 #6

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

Similar topics

3
2371
by: ewitkop90 | last post by:
Here is my code: <SCRIPT> function transportchange(transport) { if (framenewinstall.Helpdesk.checked) framenewinstall.Helpdesk.checked=false; if (framenewinstall.CircuitNumber.checked) framenewinstall.CircuitNumber.checked=false; switch (transport) { case 0: Helpdesk.innerText="No Info Needed";
1
3436
by: MickG | last post by:
I am trying to change the value of the variable "hard" according to which radio button is pressed and I am having no joy. Could anyone help me with this, the problematic section is marked with ***********************, I've included all the code incase that isn't where the problem is. Any help would be hugely appreciated. Mick
2
2894
by: Stoic | last post by:
I'm offering a selection of three items. One is $25, the second is $50, the third is $100. Along with selection one of the items I need to pass the quantity of the selection. Here is the code now. <tr><td><input type="radio" name="item_name" value="Reduced">Reduced</td><td><input type="radio" name="amount" value="25">$25.00ea.</td></tr> <tr><td><input type="radio" name="item_name" value="Regular">Regular</td><td><input type="radio"...
5
1942
by: Samy | last post by:
Hi There, I have a label in a datagrid which I make it a input type = radio in ItemDataBound so that radio buttons are shown in the datagrid. This is how I have it... ASPX... <asp:Label ID="_selectedRBtn" Runat="server"></asp:Label> ASPX.CS
1
3017
by: jw01 | last post by:
Im trying to write a java script in HTML in which when a particular radio button is selected, a textbox would appear write next to the selected item. e.g (RADIO BUTTON) fhd: (RADIO BUTTON) fdj: (RADIO BUTTON) djs: (RADIO BUTTON) hfjd: Can anyone please help me.. Thanks
2
10091
by: jw01 | last post by:
I changed my code...Now everytime i click a radio button, a text box appears...What i want is when i hit a particular radio button, the textbox should only appear next to that radio button. And if another radio button is selected, then the 1st textbox should disappear and should display in front of the new radio button selected. Following is my code: <html> <head> <script type="text/javascript">
2
2629
by: newfie912 | last post by:
I have an online application used for grading students. On one of the pages, I have a table with two rows and each row has 16 cells. The upper row contains the letter grade (A, A-, B+, B, etc) and the lower row has a 16 radio buttons for selecting the appropriate grade. This all writes to a SQL server back-end and has been working fine for several semesters. Some faculty members have complained that it is hard to read and see which...
2
1721
by: xoinki | last post by:
hi all, I am just trying to identify which radio button is selected in JS. although the code in totality does not make any sense since this is just a sample. it is not able to identify the 's' name as child element of the form. here is the code <html> <head> <script language = "javascript"> window.onload = function() {
3
3661
by: kiranbabu | last post by:
<html> <head> <h2 align=center>Blank Tapes Status Form</h2> <br><title>Blank tapes status</title> </head> <body> <br><br> <form name=tapes_status method=post>
0
9628
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
10122
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
10061
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
9923
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...
1
7471
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5368
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
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2860
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.