jquery when to use prop vs attr code example
Example: 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 );
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