Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to check if a javascript checkbox array element is checked or not?
    primarykey
    data
    text
    <p>What is want is - when the checkbox is checked option no. 5 in select list will be selected and when the checkbox is unchecked option no. 0 will be selected in the select list.</p> <p>The select list and the checkboxes are generated dynamically in the php code as below :</p> <pre><code>echo "&lt;select name='coupons".$i."' id='coupons".$i."'&gt;"; ------- All Options -------- echo "&lt;/select&gt;"; &lt;input type='checkbox' name='myCheckbox[]' value='&lt;?php echo $i."_".$row-&gt;id; ?&gt;' onclick='setCCode("myCheckbox[]",&lt;?php echo $i;?&gt;)'&gt; ----------------------------------------------------------------------------- </code></pre> <p>Solved the second requirement on my own now ..... thanks to all for your inputs</p> <p>just added the following line in the <code>checkAll()</code> within the <code>for loop</code></p> <pre><code>setCCode(children[i],i+1); </code></pre> <p>The javascript function :</p> <pre><code>function setCCode(checkbox_name,i) { var form_object = document.getElementsByName(checkbox_name+"["+i+"]"); var selname = document.getElementsByName("coupons"+i)[0]; if(form_object.checked) { selname.selectedIndex = 5; } else { selname.selectedIndex = 0; } } </code></pre> <p>The above issue is solved....... thanks to all </p> <p>Now what i need to do is when a user checks a checkbox to select or deselect all the dynamically generated checkboxes on the form the above logic should work.</p> <pre><code>&lt;input type='checkbox' name='checkall' onChange="checkAll(this, 'myCheckbox[]')"&gt; &lt;span class="chkall"&gt;Check / Uncheck All&lt;/span&gt; &lt;input type='checkbox' name='myCheckbox[]' value='&lt;?php echo $i."_".$row-&gt;id; ?&gt;' onclick='setCCode(this,&lt;?php echo $i;?&gt;)'&gt; </code></pre> <p>The javascript code i am using to select/deselect all checkboxes on form is as below :</p> <pre><code>function checkAll(parent, field) { var children = document.getElementsByName(field); var newValue = parent.checked; for (i = 0; i &lt; children.length; i++){ if (children[i].disabled == false) { children[i].checked = newValue; } } } function setCCode(Sender,i) { document.getElementsByName("coupons"+i)[0].selectedIndex = Sender.checked ? 5 : 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload