HighDots Forums  

Re: check if radio is checked

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: check if radio is checked in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Diogenes
 
Posts: n/a

Default Re: check if radio is checked - 01-02-2008 , 04:42 AM






David Mark wrote:
Quote:
Best to use:
Or:
document.forms.myform.elements.owned_business[0].checked
And this syntax is very readable once you become used to it.

If one thoughtfully creates names (or id's) that reveal
the context of the object, you will be able to read your
code 2 years later and immediately understand what you were
doing back then.

This creative naming process can be APINT too; it's so easy to
defer this mode of thinking and just use X or Y instead.

But good programming is hard work, IMHO, or at least it should be.

Cheers
-Dio






Reply With Quote
  #2  
Old   
Anthony Levensalor
 
Posts: n/a

Default Re: check if radio is checked - 01-02-2008 , 01:53 PM






Ron Eggler said:

Quote:
Hi,

I would like to run a jvascript, checking if the radio button is checked.
My radio button looks like:

input type="radio" name="owned_business" value="yes"

and i would like to check it with this:

alert (document.myform.owned_business.checked);
if (document.myform.owned_business.checked == false)

but alert returns me "undefined" and i don't understand why, my form name
is "myform" and the name of the radio is "owned_business" so i'm not seeing
where i go wrong.
Can anyone help me?

Thanks,
Ron

Try
alert(document.forms['myform']['owned_business'][0].checked);

Radios in my experience are accessed just like arrays, because the
browsers expect more than one. Incidentally, why not a checkbox?

~A!


Reply With Quote
  #3  
Old   
Anthony Levensalor
 
Posts: n/a

Default Re: check if radio is checked - 01-02-2008 , 04:35 PM



Ron Eggler said:


Quote:
Thanks
Ron

Np, you're welcome.

~A!


Reply With Quote
  #4  
Old   
David Mark
 
Posts: n/a

Default Re: check if radio is checked - 01-02-2008 , 10:08 PM



On Jan 2, 2:53*pm, Anthony Levensalor <killf... (AT) mypetprogrammer (DOT) com>
wrote:
Quote:
Ron Eggler said:





Hi,

I would like to run a jvascript, checking if the radio button is checked..
My radio button looks like:

input type="radio" name="owned_business" value="yes"

and i would like to check it with this:

* * * * alert (document.myform.owned_business.checked);
* * * * if (document.myform.owned_business.checked == false)

but alert returns me "undefined" and i don't understand why, my form name
is "myform" and the name of the radio is "owned_business" so i'm not seeing
where i go wrong.
Can anyone help me?

Thanks,
Ron

Try
alert(document.forms['myform']['owned_business'][0].checked);
Best to use:

document.forms['myform'].elements['owned_business'][0].checked

Or:

document.forms.myform.elements.owned_business[0].checked



Reply With Quote
  #5  
Old   
Anthony Levensalor
 
Posts: n/a

Default Re: check if radio is checked - 01-03-2008 , 06:45 PM



David Mark said:

Quote:
Best to use:

document.forms['myform'].elements['owned_business'][0].checked

Or:

document.forms.myform.elements.owned_business[0].checked


Why?

--
anthony at my pet programmer dot com


Reply With Quote
  #6  
Old   
David Mark
 
Posts: n/a

Default Re: check if radio is checked - 01-03-2008 , 07:29 PM



On Jan 3, 7:45*pm, Anthony Levensalor <killf... (AT) mypetprogrammer (DOT) com>
wrote:
Quote:
David Mark said:

Best to use:

document.forms['myform'].elements['owned_business'][0].checked

Or:

document.forms.myform.elements.owned_business[0].checked

Why?

Because that is the standard and most compatible syntax.



Reply With Quote
  #7  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: check if radio is checked - 01-04-2008 , 08:21 AM



Anthony Levensalor wrote:
Quote:
David Mark said:
Best to use:

document.forms['myform'].elements['owned_business'][0].checked

Or:

document.forms.myform.elements.owned_business[0].checked

Why?
The former adheres to the W3C DOM Level 2 HTML Specification while
maintaining backwards compatibility to "DOM Level 0":

http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-26809268
http://www.quirksmode.org/js/dom0.html
http://docs.sun.com/source/816-6408-10/document.htm

Whether or not the latter also complies with W3C DOM Level 2 HTML is a
matter for debate; I say "no", because the ECMAScript-binding section of the
Specification does not define it this way; other people have said "yes" with
the rationale that both property access syntaxes are equal for identifiers
in ECMAScript implementations (ES3 Final, 11.2.1), and that the
specification of this equality is outside the scope of the W3C DOM Level 2
HTML Specification.

http://www.w3.org/TR/DOM-Level-2-HTM...t-binding.html


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
// Plone, register_function.js:16


Reply With Quote
  #8  
Old   
micrexp@gmail.com
 
Posts: n/a

Default Re: check if radio is checked - 01-05-2008 , 09:53 AM



On 1月4日, 上午9时29分, David Mark <dmark.cins... (AT) gmail (DOT) com> wrote:
Quote:
On Jan 3, 7:45 pm, Anthony Levensalor <killf... (AT) mypetprogrammer (DOT) com
wrote:

David Mark said:

Best to use:

document.forms['myform'].elements['owned_business'][0].checked

Or:

document.forms.myform.elements.owned_business[0].checked

Why?

Because that is the standard and most compatible syntax.
There is the best use


<input type="radio" name="r" value = "1" id="radio1" />

First ,marked the element by "id" property

Then use this expression to find it

<script language="javascript">
var myradio = document.getElementById("radio1");
alert(myradio.checked);
</script>


Reply With Quote
  #9  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: check if radio is checked - 01-05-2008 , 11:59 AM



micrexp (AT) gmail (DOT) com wrote:
Quote:
On 1鏈4鏃, 涓婂崍9鏃29鍒, David Mark <dmark.cins... (AT) gmail (DOT) com> wrote:
[...] Anthony Levensalor [...] wrote:
David Mark said:
Best to use:
document.forms['myform'].elements['owned_business'][0].checked
Or:
document.forms.myform.elements.owned_business[0].checked
Why?
Because that is the standard and most compatible syntax.

There is the best use
No, certainly it is not.

Quote:
input type="radio" name="r" value = "1" id="radio1" /
That would be XHTML, a language that is not universally supported on the Web
as of yet.

Quote:
First ,marked the element by "id" property

Then use this expression to find it

script language="javascript"
Your markup is not even valid and you are actually talking about "best use"?

Quote:
var myradio = document.getElementById("radio1");
That would require the UA to implement this method of W3C DOM Level 2+ Core
*properly*. Especially Internet Explorer is known not to do that, as we
discussed numerous times before:

http://pointedears.de/scripts/test/dom/names-and-ids

Quote:
alert(myradio.checked);
The return value of the method should be tested before it is used. alert()
is a method of Window objects and should be called so -- window.alert(...).

Please attain a minimum amount of basic knowledge before you post more of
your "recommendations".


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16


Reply With Quote
  #10  
Old   
Steve Swift
 
Posts: n/a

Default Re: check if radio is checked - 01-05-2008 , 10:49 PM



Thomas 'PointedEars' Lahn wrote:
Quote:
Please attain a minimum amount of basic knowledge before you post more of
your "recommendations".
Just a philosophical point: "best" and "recommendations" are both
entirely subjective and require little or even no prior knowledge.

My dog could make best recommendations, but that would usually imply the
things that smell most like sweaty feet (her favourite; there's no
accounting for taste).

I have almost no knowledge of Javascript, but still know how I'd
recommend something be done, and what I find best.

--
Steve Swift
http://www.swiftys.org.uk/swifty.html
http://www.ringers.org.uk


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.