473,511 Members | 12,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get radiobox value

Hello,

I want to check if user select one from the radiobox group. How can I get
the radiobox actual value in javascript. In the value field I see the
default value, but how can I know if it really was selected?

Thanks!

<input name="menu_order" id = "menu_order1" type="radio" value="1">one<br>
<input name="menu_order" id = "menu_order2" type="radio" value="2">two<br>

Jul 23 '05 #1
13 6178

I want to check if user select one from the radiobox group.


I'd use the term radio button group.

Examine:

<html>
<head>
<title>Radio button value</title>
<script type="text/javascript">

function validate()
{
alert("We are in function validate");
var checkedButton;
var allOK= false;
var x = document.forms["myForm"];

// Figure out which radio button was pressed

if (x.receiveVia[0].checked == true)
{
checkedButton = x.receiveVia[0].value;
allOK = true;
}
else if (x.receiveVia[1].checked == true)
{
checkedButton = x.receiveVia[1].value;
allOK = true;
}
else
{
alert("button not pressed");
checkedButton = '';
allOK = false;
}
alert("checkedButton = " + checkedButton +
"; allOK = " + allOK);

return allOK;

}
</script>

</head>

<body>

<p>Please try out our form.</p>

<form name="myForm"
action="http://www.nonamedomain.com"
method="POST"
onsubmit="alert('submitting');return validate();">

<p><input type="radio" name="receiveVia" value="printed">&nbsp;Printed
brochure</p>
<p><input type="radio" name="receiveVia" value="byEmail" ">&nbsp;Via
Email</p>

</p>
<p><input type="submit" value="Submit">
</p>

</form>

</body>
</html>
Robert
Jul 23 '05 #2
Robert wrote:

I want to check if user select one from the radiobox group.


I'd use the term radio button group.

Examine:

<html>
<head>
<title>Radio button value</title>

<script type="text/javascript">

function validate()
{
alert("We are in function validate");
var checkedButton;
var allOK= false;
var x = document.forms["myForm"];

// Figure out which radio button was pressed

if (x.receiveVia[0].checked == true)
{
checkedButton = x.receiveVia[0].value;
allOK = true;
}
else if (x.receiveVia[1].checked == true)
{
checkedButton = x.receiveVia[1].value;
allOK = true;
}
else
{
alert("button not pressed");
checkedButton = '';
allOK = false;
}
alert("checkedButton = " + checkedButton +
"; allOK = " + allOK);

return allOK;

}
</script>

</head>

<body>

<p>Please try out our form.</p>

<form name="myForm"
action="http://www.nonamedomain.com"
method="POST"
onsubmit="alert('submitting');return validate();">

<p><input type="radio" name="receiveVia" value="printed">&nbsp;Printed
brochure</p>
<p><input type="radio" name="receiveVia" value="byEmail" ">&nbsp;Via
Email</p>

</p>
<p><input type="submit" value="Submit">

</p>

</form>

</body>
</html>

Robert


More generic solution that works with any group of radio buttons:

function getRadioValue(formName, radioInputName) {
var f = document.forms[formName];
if (f) {
f = f.elements[radioInputName];
}
if (f) {
if (typeof f.length == 'undefined') {
f = [ f ];
}
for (var i = 0; i < f.length; i++) {
if (f[i].checked) {
return f[i].value;
}
}
}

return null;
}

function validate(f) {
if (getRadioValue('myForm', 'receiveVia') == null) {
alert("You haven't selected a \"Receive Via\".");
return false;
}

return true;
}

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #3
Robert wrote:

Who wrote that? Please provide proper attribution.
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
I want to check if user select one from the radiobox group.


I'd use the term radio button group.

Examine:
[...]
function validate()
{
alert("We are in function validate");
var checkedButton;
var allOK= false;
var x = document.forms["myForm"];

// Figure out which radio button was pressed

if (x.receiveVia[0].checked == true)
{
checkedButton = x.receiveVia[0].value;
allOK = true;
}
else if (x.receiveVia[1].checked == true)
{
checkedButton = x.receiveVia[1].value;
allOK = true;
}
else
{
alert("button not pressed");
checkedButton = '';
allOK = false;
}
alert("checkedButton = " + checkedButton +
"; allOK = " + allOK);

return allOK;

}


That code does not scale well. Radio buttons, i.e. "input" elements
with type="radio" of a group need to have the same name. In the DOM,
that results in an elements collection (HTMLCollection object), thus
the following is better:

function getCheckedRadio(
/** @argument HTMLFormElement */ oForm,
/** @argument string */ sGroup)
/**
* @author
* Copyright (C) 2004 Thomas Lahn <dh******@PointedEars.de>
* @returns
* null, if @{(oForm)} is invalid or there is no such @{(sGroup)}<br>
* false, if no radio button of @{(sGroup)} is checked<br>
* A HTMLInputElement reference to the checked radio button otherwise
*/
{
var result = null, e, ig;
if (oForm
&& (e = oForm.elements)
&& (ig = e[sGroup]))
{
result = false;
for (var i = 0, len = ig.length, io = null;
i < len;
i++)
{
if ((io = ig[i]).checked)
{
result = io;
break;
}
}
}

return result;
}

var x;
if ((x = getCheckedRadio(document.forms[0], "receiveVia")))
{
alert("checkedButton = " + x.value);
}
else if (x == false)
{
alert("No button is checked.");
}
else
{
alert("No such form or button group.");
}
PointedEars
Jul 23 '05 #4
Thomas 'PointedEars' Lahn <Po*********@nurfuerspam.de> wrote in message news:<40**************@PointedEars.de>...
That code does not scale well.
True. It is as example of how to access the radio buttons. I figured
the OP could optimize it if it was desired.

* Copyright (C) 2004 Thomas Lahn <dh******@PointedEars.de>


Nice function, but you copyrighted it, so we cannot use it. How
useful is that?

Robert
Jul 23 '05 #5
Robert wrote:
Thomas 'PointedEars' Lahn [...] wrote [...] ...
* Copyright (C) 2004 Thomas Lahn <dh******@PointedEars.de>

Nice function, but you copyrighted it, so we cannot use it.


Is that so? That line was simply intended to declare my authorship.
How useful is that?


AIUI you can still ask me if you can use it.
PointedEars
Jul 23 '05 #6
Thomas 'PointedEars' Lahn wrote:
Robert wrote:
Thomas 'PointedEars' Lahn [...] wrote [...] ...
* Copyright (C) 2004 Thomas Lahn <dh******@PointedEars.de>


Nice function, but you copyrighted it, so we cannot use it.


Is that so? That line was simply intended to declare my authorship.

<snip>

It seems to me that an original work (including computer code) would
remain the intellectual property of its creator regardless of being
published in a public forum.

Richard.

Jul 23 '05 #7
Richard Cornford wrote:
Thomas 'PointedEars' Lahn wrote:
Robert wrote:
Thomas 'PointedEars' Lahn [...] wrote [...] ...
* Copyright (C) 2004 Thomas Lahn <dh******@PointedEars.de>
Nice function, but you copyrighted it, so we cannot use it.


Is that so? That line was simply intended to declare my authorship.

<snip>

It seems to me that an original work (including computer code) would
remain the intellectual property of its creator regardless of being
published in a public forum.


Sure. But what does this mean in this case?
PointedEars
Jul 23 '05 #8
Thomas 'PointedEars' Lahn wrote:
Richard Cornford wrote:
Thomas 'PointedEars' Lahn wrote:
Robert wrote:
Thomas 'PointedEars' Lahn [...] wrote [...] ...
> * Copyright (C) 2004 Thomas Lahn <dh******@PointedEars.de>

Nice function, but you copyrighted it, so we cannot use it.

Is that so? That line was simply intended to declare my
authorship. <snip>


It seems to me that an original work (including computer code) would
remain the intellectual property of its creator regardless of being
published in a public forum.


Sure. But what does this mean in this case?


It means that an explicit assertion of copyright/authorship doesn't
really modify the right (or lack or right) of third parties to use that
code without permission from its creator. That there is no difference
between code posted with an explicit copyright statement and code posted
without one because intellectual property rights follow from the
creation of an original work implicitly.

In practice there is very little in browser scripting that is new and
unique. Your function is reminiscent of code associated with the FAQ;
differently structured, particularly in its feature detecting, but
essentially the same process. In principle I own the rights to that
(specific) code from the FAQ[1], but if someone wrote more code that did
essentially the same thing again, it would probably not be possible to
determine whether it was based on either of ours, stolen verbatim form a
third party, or created out of the imagination of its author without
direct external influence.

Richard.

[1] Or do I? I wrote that code:-

<URL: http://jibbering.com/faq//faq_notes/...ess.html#faBut >

- but I cannot tell to what extent it is original. The problem is so
limited, and the applicable logic so obvious, that any programmer in a
similar situation would write similar code.

The next block of code on that page is based on an Idea that I first
encountered in posts by Grant Wagner and so could not be clamed to be
original (except in the implementation details, and probably not even
then). I recall thinking at the time that it was obviously a good idea
under those circumstances, but such an obvious good idea that if I had
encountered that actual situation prior to that point it might have
occurred to me as applicable anyway.

While the block that follows it certainly is my invention (I recall
thinking through the logic of the tests) but it probably isn't actually
unique or original either.
Jul 23 '05 #9
Richard Cornford wrote:
Thomas 'PointedEars' Lahn wrote:
Richard Cornford wrote:
Thomas 'PointedEars' Lahn wrote:
Robert wrote:
> Thomas 'PointedEars' Lahn [...] wrote [...] ...
>> * Copyright (C) 2004 Thomas Lahn <dh******@PointedEars.de>
> Nice function, but you copyrighted it, so we cannot use it.

Is that so? That line was simply intended to declare my
authorship. <snip>

It seems to me that an original work (including computer code) would
remain the intellectual property of its creator regardless of being
published in a public forum.
Sure. But what does this mean in this case?


It means that an explicit assertion of copyright/authorship doesn't
really modify the right (or lack or right) of third parties to use that
code without permission from its creator.


Full ACK.
That there is no difference between code posted with an explicit
copyright statement and code posted without one because intellectual
property rights follow from the creation of an original work implicitly.
Apparently it is a bit more difficult than that:
<http://www.whatiscopyright.org/>

So AIUI there was and is nothing wrong with such a line. It merely says
who originally made this (or contributed to it) and thus whom (and where)
one can turn to for issues with it, i.e. it declares the authorship. (And
no, I do not have registered that code at the U.S. Copyright Office ;-))
In practice there is very little in browser scripting that is new and
unique. Your function is reminiscent of code associated with the FAQ;
I did not know of that.
differently structured, particularly in its feature detecting, but
essentially the same process.


True. But to make that clear: It is _not_ the *same* code which is
supported by the fact that the code in the FAQ is also not as efficient as
mine is (I use variable references wherever possible instead of looking up
again); so *if* there in the future would be restrictions set for the FAQ
code (which would seem contradictory as a FAQ is intended to be used) it
would not impact my rights regarding my code. It is a *different* piece
of work.

(Don't worry, once the dhtml.js library is updated with it my function will
soon get GPL'd as almost every code I have written anyway, so one can use
it *freely* as long as the authorship chain is still mentioned and you take
heed of the other agreements of the GNU GPL [I already have a similar, older
one in it but that only returns the value which turned out not to be
sufficient]. I strongly object to software patents.)
PointedEars, IANAL
Jul 23 '05 #10
JRS: In article <c9*******************@news.demon.co.uk>, seen in
news:comp.lang.javascript, Richard Cornford
<Ri*****@litotes.demon.co.uk> posted at Thu, 3 Jun 2004 16:26:52 :
Thomas 'PointedEars' Lahn wrote:
Robert wrote:
Thomas 'PointedEars' Lahn [...] wrote [...] ...
* Copyright (C) 2004 Thomas Lahn <dh******@PointedEars.de>
Nice function, but you copyrighted it, so we cannot use it.


Is that so? That line was simply intended to declare my authorship.

<snip>

It seems to me that an original work (including computer code) would
remain the intellectual property of its creator regardless of being
published in a public forum.


That is so. The Copyright notice is really little more than a customary
reminder.

By posting here, the methods are disclosed and are available for re-use
in any manner. It is the form of the expression that is covered by
copyright.

Copying the code exactly for use in a Web page should normally be
considered fair expected practice; but copying the whole in a medium
designed to be read by people should not be.

Where there is only one reasonable way of doing something, then
publication does not prohibit republication.

The FAQ has a couple of links on Copyright; there are more at <URL:http:
//www.merlyn.demon.co.uk/"misclinx.htm#Copy>.

IANAL.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #11
Thomas 'PointedEars' Lahn wrote:
Richard Cornford wrote: <snip>
That there is no difference between code posted with an
explicit copyright statement and code posted without one
because intellectual property rights follow from the
creation of an original work implicitly.


Apparently it is a bit more difficult than that:
<http://www.whatiscopyright.org/>


Isn't that always the way? :)
So AIUI there was and is nothing wrong with such a line.
I don't have a problem with it. I suppose some Usenet zealots might
complain about the needless consumption of bandwidth.

<snip>
In practice there is very little in browser scripting that
is new and unique. Your function is reminiscent of code
associated with the FAQ;


I did not know of that.
differently structured, particularly in its feature detecting,
but essentially the same process.


True. But to make that clear: It is _not_ the *same* code


No it is not the same code. But it is very similar code, because both
are derived from the logic that must be applied to the situation. The
underlying algorithm:-

1. Get the radio button collection.
2. Loop through the collection until a button is found
with a - true - checked property, or the collection
runs out.
3. Return a reference to that button if one was found.

- is probably the only one applicable to the problem, and it is so
simple that all implementations will be similar.
which is supported by the fact that the code in the
FAQ is also not as efficient as mine is (I use variable
references wherever possible instead of looking up
again);
To some extent that is true. Re-resolving the - radioCollection.length -
property for each evaluation of the test expression is less efficient
than assiginig the colleciton length to a local variable as you did in:-

| for (var i = 0, len = ig.length, io = null;
| i < len;
| i++)
| {
...
| }

However:-

for(var i = ig.length;i--;){
...
}

- allows for only one resolution of the collection length property and
avoids the variable instantiation and assignment of the - len -
variable. It can be applicable in this context because there is no
reason to prefer to loop through the collection in ascending order over
descending.

But for ultimate performance in execution I would expect:-

var i = ig.length;
if(i--){
do{
...
}while(i--);
}

- to have the edge as - do-while - is more amenable to optimisation
that - for - loops (even with the surrounding - if - test).

But I was motivated to use the less efficient loop in the FAQ because it
is clearer (at least for less experienced/knowledgeable programmers).

The second use of a local variable to avoid re-resolution of a property
accessor:-

| if ((io = ig[i]).checked)
| {
| result = io;
| break;
| }

- may even be counterproductive because it happens within the - for -
loop. Compared with my:-

# if(radioCollection[c].checked){
# checkedButton = radioCollection[c];
# break;
# }

Certainly your assignment of - io - to - result - is faster than my
assignment of - radioCollection[c] - to - checkedButton - because of the
additional need to resolve - c - as a property of - radioCollection -,
but that only happens once. While your - if - expression must be slower
because of the additional assignment. And that expression is evaluated
on every iteration of the loop.

The bigger the radio button collection the longer the loop and the more
the accumulated penalty of the additional assignment, in return for a
small advantage that only happens once.
so *if* there in the future would be restrictions set for
the FAQ code (which would seem contradictory as a FAQ is
intended to be used)
For the record, any code that I have contributed to the FAQ and/or the
notes may be used by anyone who wants to (at their own risk and without
warranty of any kind).

<snip> ... . I strongly object to software patents.)


Currently software patenting seems very silly in terms of what is
happening, but if someone invents (and I would insist on seeing an
implementation, not just a concept) something _new_ and unique, I can't
see why they shouldn't patent it.

Richard.
Jul 23 '05 #12
JRS: In article <c9*******************@news.demon.co.uk>, seen in
news:comp.lang.javascript, Richard Cornford
<Ri*****@litotes.demon.co.uk> posted at Thu, 3 Jun 2004 18:45:57 :
In principle I own the rights to that
(specific) code from the FAQ[1], [1] Or do I? I wrote that code:-

<URL: http://jibbering.com/faq//faq_notes/...ess.html#faBut >

- but I cannot tell to what extent it is original.

Copyright was discussed a while back in relation to the clpdmFAQ (see
below).

AFAIR, it was agreed that copyright for that newsgroup FAQ actually
belonged to the newsgroup, being held in trust by the current agreed
maintainer; and that an offer of original code for the FAQ should be
deemed to include donation of copyright.

That seems to me to be the correct policy for a newsgroup FAQ.

Some of the discussion was in mail, but much in News; September 2003 or
thereabouts, subject 'miniFAQ updates' - Google should have it.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Delphi 3 Turnpike 4 ©
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htm> clpdmFAQ;
<URL:http://www.borland.com/newsgroups/guide.html> news:borland.* Guidelines
Jul 23 '05 #13
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
AFAIR, it was agreed that copyright for that newsgroup FAQ actually
belonged to the newsgroup, being held in trust by the current agreed
maintainer; and that an offer of original code for the FAQ should be
deemed to include donation of copyright.
While I agree completely with the sentiment, I'm not sure it won't
hold up in court though. A newsgroup is not a legal entity, and cannot
hold a copyright, and copyright can not be expected to be donated
without explicit consent of the author.
That seems to me to be the correct policy for a newsgroup FAQ.


Absolutely, and I doubt there will be anyone complaining about the use
of the content of the FAQ.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #14

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

Similar topics

6
2381
by: Austin | last post by:
Every time i use wxRadioBox, it will show the box. I just want the options show on the panel. What could i do?
1
14124
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of...
3
18182
by: otto | last post by:
i need to read a variable in a javascript and translate it to a form in html the javascript variable is: <SCRIPT LANGUAGE='JavaScript'>RF2N('Total');</script> and i need to put that...
3
11915
by: Eric Chang | last post by:
I was working on this simple form with radio boxes. And when I click on one of the radio box, it tell me the value is "undefined" Why is that ? I did defined the value of each radio box: ...
16
11463
by: cwizard | last post by:
I'm calling on a function from within this form, and there are values set but every time it gets called I get slammed with a run time error... document.frmKitAmount.txtTotalKitValue is null or not...
1
1189
by: Child | last post by:
I am having errors capturing both the value and the text of my radio box list selections: the list looks like this: <asp:RadioButtonList id="rblReferralFollowThrough" runat="server">...
0
856
by: wei | last post by:
Hello All I want to add RadioBox control or DropDownList control to my login page, so the user can choose which database they want to use, but after I added RadioBox control, It shows me the name"...
2
4227
by: crystalattice | last post by:
In my GUI app, I have a radio box allowing the user to pick his/her gender. When I click a button, I'd like the radio box to tell the program which choice is marked. I can get it to work when I...
2
1785
by: krisvt | last post by:
Hi. I'm trying to make a PHP script that simply prints some stored information after the user has checked a radiobox and presses submit. Have been many years since I have done any of this so...
0
7251
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
7148
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
7367
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
5673
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,...
1
5072
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...
0
4743
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
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
451
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...

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.