javascript - jQuery .prop('checked', false) does not work -


html:

<form class="form">     <input type="checkbox" id="box" /> check me! </form> 

js:

$( window ).load( function() {      var myfunc = function() {          if( $( '#box' ).prop( 'checked', false ) ) {             $( '.form' ).append( '<p>checkbox not checked.</p>' );         }      }      $( '#box' ).on( 'change', myfunc );  } ); 

here jsfiddle http://jsfiddle.net/3pym7/

when use $( '#box' ).prop( 'checked', false ) condition if statement not work, ! $( '#box' ).prop( 'checked' ) works fine!

the statement $('#box').prop('checked', false) not return boolean rather set checked property false should not used in condition , normal behaviour

if($('#box').prop('checked', false)) 

could changed test using is() :checked selector.

if($('#box').is(':checked')) 

or

if($('#box:checked').length) 

you can get best performance using native javascript

if(document.getelementbyid('box').checked) 

the working statement $('#box').prop('checked') mentioned returns checked property value instead of setting.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -