Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check a group of checkboxes based on another group of checkboxes with JQuery
    primarykey
    data
    text
    <p>I have two separate forms on the page and both of them have a group of checkboxes.</p> <pre><code>&lt;body&gt; &lt;form name="form1" id="form1" ...&gt; &lt;input type="checkbox" name="check1" value="Person" /&gt; &lt;input type="checkbox" name="check1" value="Dog" /&gt; &lt;input type="checkbox" name="check1" value="Cat" /&gt; &lt;/form&gt; &lt;form name="form2" id="form2" ...&gt; &lt;input type="checkbox" name="check1" value="Person" /&gt; &lt;input type="checkbox" name="check1" value="Dog" /&gt; &lt;input type="checkbox" name="check1" value="Cat" /&gt; &lt;/form&gt; &lt;/body&gt; </code></pre> <p>What I need here is, once the user check/unchecks any checkbox in form2, I want to do the same for corresponding checkbox in form1. If the user selects "dog" and "cat" in form2, check the same for form1 automatically. I have tried a couple of things but I can't figure out a way to set checked attribute based on the value field.</p> <p>Best I could to do is to have unique ids for all checkboxes and select/unselect based on ids. But I am trying to find out if there is a better selector way.</p> <p>Also, I will be glad if anyone can suggest me a better way to do what I am trying here. </p> <p>-- I haven't tried this yet, but this is what I was planning to do---</p> <pre><code> &lt;form name="form1" id="form1" ...&gt; &lt;input type="checkbox" name="check1[]" id="form1_Person" value="Person" /&gt; &lt;input type="checkbox" name="check1[]" id="form1_Dog" value="Dog" /&gt; &lt;input type="checkbox" name="check1[]" id="form1_Cat" value="Cat" /&gt; &lt;/form&gt; &lt;form name="form2" id="form2" ...&gt; &lt;input type="checkbox" name="check1[]" id="form2_Person" value="Person" /&gt; &lt;input type="checkbox" name="check1[]" id="form2_Person" value="Dog" /&gt; &lt;input type="checkbox" name="check1[]" id="form2_Person" value="Cat" /&gt; &lt;/form&gt; </code></pre> <p>once form2 checkbox is clicked, get the value, and look for checkbox in form1. If dog is selected, check $('#form1_'[dog]).checked('checked',checked)</p>
    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.
 

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