What is the easiest way to read the value of a checkbox?

R

Randell_D

I know I can read the value of an input text box via
document.form['myformname'].elements['myfieldname'].value (and a
number of other ways) but when I try to read checkbox value using the
above method it doesn't work for me.

My question to your fineselves is: What is the easiest way for me to
read the value of a checkbox?

It sounds simple but I've not written js in about six or more months
so I'm blank on ideas...

Thanks!
 
M

Martin Honnen

Randell_D said:
I know I can read the value of an input text box via
document.form['myformname'].elements['myfieldname'].value (and a
number of other ways) but when I try to read checkbox value using the
above method it doesn't work for me.

If you have a single form control with name="myfieldname" then the above
should work (with document.forms not document.form). If you have more
than one form control with that name then you need to index the second
collection
document.forms['myformname'].elements['myfieldname'][0].value
for the first.
 
M

M5v0h4y1

I know I can read the value of an input text box via
document.form['myformname'].elements['myfieldname'].value (and a
number of other ways) but when I try to read checkbox value using the
above method it doesn't work for me.

My question to your fineselves is: What is the easiest way for me to
read the value of a checkbox?

It sounds simple but I've not written js in about six or more months
so I'm blank on ideas...

Thanks!

I think I'm having a similar problem. You suggest that I can read the
value of an input text box via
document.form['myformname'].elements['myfieldname'].value. I try to
use this to set the value in a menu list through a
document.write('<option
value="'+document.form['myformname'].elements['myfieldname'].value'">
Other - Employer Not Listed</option>'), yet the option item doesn't
show up in the menu and the value isn't set.
 
E

Evertjan.

Randell_D wrote on 24 mrt 2008 in comp.lang.javascript:
I know I can read the value of an input text box via
document.form['myformname'].elements['myfieldname'].value (and a
number of other ways) but when I try to read checkbox value using the
above method it doesn't work for me.

It would work for me, changing .form[] to .forms[].

what is "doesn't work for me"?
 
R

Randell_D

document.forms['myformname'].elements['myfieldname'][0].value
for the first.

Luvely juvely... I forgot that I had to loop thru them and my js
pocket reference from o'reilly didn't make this clear for me... Thanks
for pointing me in the right direction.

I have written the following snippet of code and it works for me.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<body>
<form name="form1" method="post" action="">
<p>test1:<input name="test1" type="text" id="test1"></p>
<p>radio1<input name="radiobutton" type="radio" value="1" checked>
radio2<input type="radio" name="radiobutton" value="2"></p>
</form>
<hr>
<a href="#" onClick="showValues();">Show Values</a>
<script language="JavaScript">
function readRadioButton(radioObj)
{ // Function should be passed a reference to radio object, it
returns
// with selected value else returns false
var counter=new Number(0);
var limit=radioObj.length;
for( counter=0; counter<limit; ++counter )
{ if( radioObj[counter].checked==true )
{ return radioObj[counter].value; }
}
return false;
}

function showValues()
{
radioObject=f1.elements['radiobutton'];
alert(readRadioButton(radioObject));
return true;
}
var DOCUMENT=top.document;
var f1=DOCUMENT.forms['form1'];
</script>
</body>
</html>
 
R

Randell_D

document.write('<option
value="'+document.form['myformname'].elements['myfieldname'].value'">
Other - Employer Not Listed</option>'), yet the option item doesn't
show up in the menu and the value isn't set.

No - I think you are having a different problem - The <option> tag
relates to a select box (for example a pull down menu with scroll bar)
and not to a radio/checkbox - these are handled differently. You are
complicating things for both you and your browser by using
document.write to achieve your aims...

I don't mean to be rude but only use code that you know you can
support - I provide a solution for you below but I strongly suggest
you get a good book or surf the web for some js guides as you'll only
end up pulling your hair out. Javascript is not too difficult if you
can script in any other language (like unix shell scripting), php,
pearl or even c/c++... The problem with javascript is making something
cross-browser compatable.

I suggest you read a book from O'Reilly guides but others are good
too.

function append_new_option(select_box_obj,addtext,addvalue)
{ // Add a previously non-existing option to a select box
select_box_obj.options[select_box_obj.options.length]=new
Option(addtext,addvalue);
return true;
}

If your form is called "myForm" and your select box is called
"mySelectBox" and you wanted to add a new option to the select box you
could call it using

append_new_option(document.myForm.mySelectBox,"client text","server-
value");
 
R

Randell_D


Thanks for the link above btw...

I have another (quick?) question... I am writing a generic function
that reads values conditional on the form input type.

var DOCUMENT=top.document;
var f1=DOCUMENT.forms['form1'];
var fieldObj=f1.elements[fieldName];
var fieldType=fieldObj.type;

If I do the following:

alert(fieldType);

I get correct "text", "select-one", "hidden" etc etc values but when I
come to a radio or checkbox I instead get "undefined" in the alert
popup box.

My question is if I cannot I identify a field type using the above
code, then (if why is too long an answer) how else can I do it?
 
D

Dr J R Stockton

In comp.lang.javascript message <e29c435d-de4c-4b74-bc42-16daa3aab604@i1
2g2000prf.googlegroups.com>, Mon, 24 Mar 2008 09:14:56, Randell_D
I know I can read the value of an input text box via
document.form['myformname'].elements['myfieldname'].value (

Should that not be ... .checked?
 
R

Randell_D

Should that not be ... .checked?

--
(c) John Stockton, nr London, UK. [email protected] Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

Thanks... but... the ".checked" would return "true" or "false" and i
wanted the value... in my example, had I had two radio boxes named
"status", the first with a value of zero and the second with a value
of 1 I would want a one or zero.

Martin Honnen's response got me going... but I've now got a follow on
post for me to correctly identify a field type - I must be doing
something wrong and I have raised the question under the title " why
js identify my input type for text box but not for checkbox/radiobox"
or via this url (if you think you can help)

http://groups.google.com/group/comp...a90a10e43d5/3b41b3e66ba49d7b#3b41b3e66ba49d7b

BTW I note you're using Turnpike... My first ISP was Demon in 1995/96
and remember an early version of it... you brought my memory on a
journey just thinking of that...

thanks for the post/comments...
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top