What are the differences between attr() and prop () in jQuery?

There are two properties, attr and prop, and I can not understand how they differ, if you know, please write.

Author: 69 420 1970, 2018-06-17

1 answers

Let's say we need to know if input="checkbox" is pressed.
As far as we know, in order for it to be "clicked" initially, you need to add an attribute to itchecked.

So we need to check it for an attribute..
This is where you need to know the difference between . prop() and . attr().

Creating <input>
And check it for "pressing" with .attr()

$('#check').bind('change',function(){
    var check = $(this).attr('checked');
    console.log('Check? '+check);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.js"></script>

<input id="check" type="checkbox">

Now, let's check with .prop()

$('#check').bind('change',function(){
    var check = $(this).prop('checked');
    console.log('Check? '+check);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.js"></script>

<input id="check" type="checkbox">
 5
Author: CbIPoK2513, 2018-06-17 22:17:30