element attr vs prop code example
Example: Difference between prop() and attr()
<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