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

Discussion in 'Javascript' started by Randell_D, Mar 24, 2008.

  1. Randell_D

    Randell_D Guest

    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!
     
    Randell_D, Mar 24, 2008
    #1
    1. Advertising

  2. Randell_D wrote:
    > 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.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Mar 24, 2008
    #2
    1. Advertising

  3. Randell_D

    Guest

    On Mar 24, 12:14 pm, Randell_D <> wrote:
    > 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.
     
    , Mar 24, 2008
    #3
  4. Randell_D

    Evertjan. Guest

    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"?

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Mar 24, 2008
    #4
  5. Randell_D

    Randell_D Guest

    On Mar 24, 6:28 pm, Martin Honnen <> wrote:
    > 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>
     
    Randell_D, Mar 24, 2008
    #5
  6. Randell_D

    Randell_D Guest

    On Mar 24, 6:30 pm, wrote:

    > 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");
     
    Randell_D, Mar 24, 2008
    #6
  7. Randell_D

    Randell_D Guest

    On Mar 24, 6:28 pm, Martin Honnen <> wrote:
    > Martin Honnen
    > http://JavaScript.FAQTs.com/


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


    Should that not be ... .checked?

    --
    (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk 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)
     
    Dr J R Stockton, Mar 24, 2008
    #8
  9. Randell_D

    Randell_D Guest

    On Mar 25, 12:35 am, Dr J R Stockton <> wrote:
    > Should that not be ... .checked?
    >
    > --
    > (c) John Stockton, nr London, UK. ? 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...
     
    Randell_D, Mar 24, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tom
    Replies:
    4
    Views:
    14,123
    jpowers5882
    Oct 24, 2008
  2. uluda

    Easiest colors to read

    uluda, Jan 23, 2009, in forum: HTML
    Replies:
    2
    Views:
    857
    C A Upsdell
    Jan 23, 2009
  3. Raymond Schanks
    Replies:
    0
    Views:
    341
    Raymond Schanks
    Jul 15, 2010
  4. Imran Aziz

    checkbox value attribute in checkbox control ?

    Imran Aziz, Aug 6, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    230
    Imran Aziz
    Aug 8, 2005
  5. randy
    Replies:
    13
    Views:
    450
    Tad McClellan
    Nov 1, 2003
Loading...

Share This Page