Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old September 15th, 2006, 11:41 PM
Registered User
Join Date: Sep 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default get the checked value from a checkbox array

Here is my sample code:

Alert ("checked = " + document.gemOptions.elements['gem[]'].checked);

where gemOptions is the formName and gem[] is the checkbox name array.
Incidently this alert box displays 'checked = undefined'
My problem is that I cannot use this in an if statement because I cannot get the correct returned value. Is the element checked? How do I know?

Please help me.


<form name="gemOptions" method="post" action="" onsubmit="">

    Diamond <input type="checkbox" name="gem[]" value="Diamond">

    Pearl <input type="checkbox" name="gem[]" value="Pearl">

    Emerald <input type="checkbox" name="gem[]" value="Emerald">

    Ruby <input type="checkbox" name="gem[]" value="Ruby">

    Saphire <input type="checkbox" name="gem[]" value="Saphire">


        <input type="reset" value="Finished"
            onclick="javascript: ShowMenu(document.getElementsByName('gem[]'),'gemForm',document.gemOptions.elements['gem[]'])">


Old September 16th, 2006, 11:02 AM
joefawcett's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

As far as I know [] are not legal name characters but your main problem is that there's no shortcut for this task. You need to loop through the checkboxes and test each one. What do you want to know, which are checked or are any checked?
Are you sure you want checkboxes not a radio group where only one can be picked?


Joe (Microsoft MVP - XML)

Similar Threads
Thread Thread Starter Forum Replies Last Post
Checkbox checked all mateenmohd Javascript 1 January 2nd, 2007 06:19 AM
checkbox checked by default by html:checkbox sachin.tathod Struts 3 December 4th, 2006 03:41 PM
Checkbox checked? ninel ASP.NET 2.0 Professional 1 April 12th, 2006 10:50 PM
allow only one checkbox to be checked! morpheus HTML Code Clinic 5 April 15th, 2004 10:59 AM
Display Checkbox if checked or not jmss66 Classic ASP Basics 6 January 23rd, 2004 11:19 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.