Subject: get the checked value from a checkbox array
Posted By: teklline123 Post Date: 9/15/2006 11:41:49 PM
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.

eg.

<form name="gemOptions" method="post" action="" onsubmit="">
<table>
        
    <tr><td>
    Diamond <input type="checkbox" name="gem[]"  value="Diamond">
    &nbsp;&nbsp;&nbsp;
    </td>
    
    <td>
    Pearl <input type="checkbox" name="gem[]" value="Pearl">
    &nbsp;&nbsp;&nbsp;
    </td>
    
    <td>
    Emerald <input type="checkbox" name="gem[]" value="Emerald">
    &nbsp;&nbsp;&nbsp;
    </td>
    
    <td>
    Ruby <input type="checkbox" name="gem[]" value="Ruby">
    &nbsp;&nbsp;&nbsp;
    </td>
    
    <td>
    Saphire <input type="checkbox" name="gem[]" value="Saphire">
                            
                            
    &nbsp;&nbsp;&nbsp;
    </td>
    </tr>
     
    <tr>
    <td>
        <input type="reset" value="Finished"
            onclick="javascript:         ShowMenu(document.getElementsByName('gem[]'),'gemForm',document.gemOptions.elements['gem[]'])">
    </td>
    </tr>                         

</table>


Reply By: joefawcett Reply Date: 9/16/2006 11:02:27 AM
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)

Go to topic 49853

Return to index page 173
Return to index page 172
Return to index page 171
Return to index page 170
Return to index page 169
Return to index page 168
Return to index page 167
Return to index page 166
Return to index page 165
Return to index page 164