Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You want to change the way you determine if the element is <code>checked</code>:</p> <pre><code>$('#additionalDonationCheckbox').click( function() { $(this).is(':checked') ? alert('checked') : alert('unchecked'); } ); </code></pre> <p>Or</p> <pre><code>$('#additionalDonationCheckbox').click( function() { $(this).prop('checked') ? alert('checked') : alert('unchecked'); } ); </code></pre> <p>This is because <code>checked</code> is both a DOM property and a DOM attribute. The difference between them can be important in some situations (like this).</p> <p>The DOM attribute <code>checked</code> refers to the <em>initial</em> state of the checkbox. So if the checkbox was created with <code>checked="checked"</code>, the DOM attribute <code>checked</code> will always return <code>'checked'</code>, even if the checkbox has since been unchecked.</p> <p>The DOM property <code>checked</code> refers to the <em>current</em> state of the checkbox, and behaves like you want it to. It will return whether a checkbox is checked <em>now</em> regardless of whether it was checked when the document was loaded.</p> <p>It's a subtle difference confused by the fact that jQuery is inconsistent on whether <code>$.attr('checked')</code> returns the attribute or property across versions. Details can be found in the <a href="http://api.jquery.com/prop/" rel="nofollow">documentation</a>.</p> <p>This is further confused by <a href="http://bugs.jquery.com/ticket/10278" rel="nofollow">a bug</a> in jquery 1.6.3 and 1.6.4 that returns a stale value for <code>$(checkbox).attr('checked')</code> after <code>$(checkbox).click()</code>.</p> <p>In summary, if you want to be sure you are getting the <em>current</em> state of the checkbox, either use the <code>is(':checked')</code> call or the <code>prop('checked')</code> call.</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.
 

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