Get radiobox value

C

Clevo

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>
 
G

Grant Wagner

Robert said:
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.checked) {
return f.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 <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_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-developer/upgrade_2.html
 
T

Thomas 'PointedEars' Lahn

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 <[email protected]>
* @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).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
 
R

Robert

Thomas 'PointedEars' Lahn said:
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 <[email protected]>

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

Robert
 
R

Richard Cornford

Thomas said:
Robert said:
Thomas 'PointedEars' Lahn [...] wrote [...] ...
* Copyright (C) 2004 Thomas Lahn <[email protected]>

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.
 
T

Thomas 'PointedEars' Lahn

Richard said:
Thomas said:
Robert said:
Thomas 'PointedEars' Lahn [...] wrote [...] ...
* Copyright (C) 2004 Thomas Lahn <[email protected]>


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
 
R

Richard Cornford

Thomas said:
Richard said:
Thomas said:
Robert wrote:
Thomas 'PointedEars' Lahn [...] wrote [...] ...
* Copyright (C) 2004 Thomas Lahn <[email protected]>

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/form_access.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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
Richard Cornford
Thomas said:
Robert said:
Thomas 'PointedEars' Lahn [...] wrote [...] ...
* Copyright (C) 2004 Thomas Lahn <[email protected]>


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.
 
T

Thomas 'PointedEars' Lahn

Richard said:
Thomas said:
Richard said:
Thomas 'PointedEars' Lahn wrote:
Robert wrote:
Thomas 'PointedEars' Lahn [...] wrote [...] ...
* Copyright (C) 2004 Thomas Lahn <[email protected]>
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
 
R

Richard Cornford

Thomas said:
Richard Cornford wrote:

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.

I did not know of that.


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).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).

... . 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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
Richard Cornford
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/form_access.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.
 
L

Lasse Reichstein Nielsen

Dr John Stockton said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,284
Messages
2,571,414
Members
48,106
Latest member
JamisonDev

Latest Threads

Top