Answers for "prop vs attr jquery"

1

prop vs attr jquery

<input id="check1" type="checkbox" checked="checked">
<label for="check1">Check me</label>
<p></p>

$( "input" ).change(function() {
  var $input = $( this );
  //  alert($input.prop( "checked" ));
  if($input.prop( "checked" )){
  $( "p" ).html(
    ".attr( \"checked\" ): <b>" + $input.attr( "checked" ) + "</b><br>" +
    ".prop( \"checked\" ): <b>" + $input.prop( "checked" ) + "</b><br>" +
    ".is( \":checked\" ): <b>" + $input.is( ":checked" ) + "</b>" );
  }else{
    $( "p" ).html("Not Checked");
  }
}).change();


Output:
if condition is  checked
.attr( "checked" ): checked
.prop( "checked" ): true
.is( ":checked" ): true
if condition is not checked
Not Checked
Posted by: Guest on March-19-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language