AW: [Javascript] checkboxes in array

Mike Dougherty mdougherty at
Tue Mar 15 08:37:32 CST 2005

How about this:?

<label for='typ_1'>typ 1:</label>
<input type='checkbox' name='typ[]' id='typ_1' value='1' />
<label for='stck_1'>stck 1:</label>
<input type='text' name='stck[]' id='stck_1' value='4' onFocus="checkIt('typ_1');" />

<label for='typ_2'>typ 2:</label>
<input type='checkbox' name='typ[]' id='typ_2' value='2' />
<label for='stck_2'>stck 2:</label>
<input type='text' name='stck[]' id='stck_2' value='0' onFocus="checkIt('typ_2');" />


<script type='text/javscript'>
function checkIt( tcCBId ) {
   var loCB = document.getElementById( tcCBId );
   if (loCB) {
     loCB.checked = true ;

Note: the use of the <label> tag aids accessibility on the checkbox because you can click the 
label to toggle the checkbox value (rather than the little square of the checkbox itself)  the 
<label> tag also provides a convenient hook for styling - it would be even better if IE understood 
attribute selectors so your CSS could be label[for=typ_1] {rule} but whatever..  

On Tue, 15 Mar 2005 15:24:34 +0100
  "Michael Borchers" <borchers at> wrote:
>>   To Access the corresponding checkbox try using 
>> "document.getElementsByName
>> ('typ[]');" it'll return an array of the objects (the three 
>> checkboxes) that 
>> match to that name.
>and how it should look exactly?!
>typ 1:<input type="checkbox" name="typ[]" value="1"> stck 1:<input type="text" name="stck[]" 
>value="4" onFocus="document.getElementsByName('typ[]').checked=true">

More information about the Javascript mailing list