Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>jQuery 1.6+</h2> <p>Use the new <a href="https://api.jquery.com/prop" rel="noreferrer"><code>.prop()</code></a> method:</p> <pre><code>$('.myCheckbox').prop('checked', true); $('.myCheckbox').prop('checked', false); </code></pre> <h2>jQuery 1.5.x and below</h2> <p>The <code>.prop()</code> method is not available, so you need to use <a href="https://api.jquery.com/attr" rel="noreferrer"><code>.attr()</code></a>.</p> <pre><code>$('.myCheckbox').attr('checked', true); $('.myCheckbox').attr('checked', false); </code></pre> <p>Note that this is <a href="https://github.com/jquery/jquery/blob/1.5.2/test/unit/attributes.js#L157" rel="noreferrer">the approach used by jQuery's unit tests prior to version 1.6</a> and is preferable to using</p> <pre><code>$('.myCheckbox').removeAttr('checked'); </code></pre> <p>since the latter will, if the box was initially checked, change the behaviour of a call to <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement.reset" rel="noreferrer"><code>.reset()</code></a> on any form that contains it - a subtle but probably unwelcome behaviour change.</p> <p>For more context, some incomplete discussion of the changes to the handling of the <code>checked</code> attribute/property in the transition from 1.5.x to 1.6 can be found in the <a href="https://blog.jquery.com/2011/05/03/jquery-16-released/" rel="noreferrer">version 1.6 release notes</a> and the <strong>Attributes vs. Properties</strong> section of the <a href="https://api.jquery.com/prop/" rel="noreferrer"><code>.prop()</code> documentation</a>.</p> <h2>Any version of jQuery</h2> <p>If you're working with just one element, you can always just modify the <a href="https://developer.mozilla.org/en/docs/Web/API/HTMLInputElement" rel="noreferrer"><code>HTMLInputElement</code></a>'s <code>.checked</code> property:</p> <pre><code>$('.myCheckbox')[0].checked = true; $('.myCheckbox')[0].checked = false; </code></pre> <p>The benefit to using the <code>.prop()</code> and <code>.attr()</code> methods instead of this is that they will operate on all matched elements.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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