Note that there are some explanatory texts on larger screens.

plurals
  1. PORoR jQuery Check/Uncheck Specific Items
    primarykey
    data
    text
    <p>I'm currently building a RoR webpage. I'm trying to build a checkbox that helps select certain options in a form when checked. And when the check box is unchecked, it will automatically uncheck said selected options.</p> <p>For example:</p> <pre><code> &lt;form id='list'&gt;&lt;br&gt; 1&lt;input type='checkbox' value='1' /&gt; 2&lt;input type='checkbox' value='2' /&gt; 3&lt;input type='checkbox' value='3' /&gt; 4&lt;input type='checkbox' value='4' /&gt; 5&lt;input type='checkbox' value='5' /&gt; CheckAnimal&lt;input type='checkbox' name='checkanimal' onclick='checkAnimal()'&gt;&lt;br&gt; &lt;/form&gt; </code></pre> <p>And JavaScript:</p> <pre><code> &lt;script language='JavaScript'&gt; function checkAnimal () { var values = ['1', '2', '4', '5']; if (checked == false){checked = true}else{checked = false} $("#list").find('[value=' + values.join('], [value=') + ']').prop("checked", true); } &lt;/script&gt; </code></pre> <p>In JavaScript, without <code>if (checked == false){checked = true}else{checked = false}</code>, the options (1,2,4,5) will be selected when clicking on "CheckAnimal" checkbox. However, if I add the code back, the checkbox will not work at all. </p> <p>By the way, these codes only work in JSFiddle and doesn't work in my local environment and I have no idea why.</p> <p>JSFiddle: <a href="http://jsfiddle.net/yddq6/" rel="nofollow">http://jsfiddle.net/yddq6/</a></p> <p>The codes are all in document "_form.html.erb" and Google CDN (jQuery 1.10.2) <code>&lt;head&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"&gt;&lt;/script&gt; &lt;/head&gt;</code>) is in the .erb file as well. </p> <p>Please kindly help!</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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