In this code, we can obtain value of element id as 2 differnt way and both way return resultat
$("#my_div").on("click", function () {
alert( $(this).attr("id") );
alert( this.id );
});
But i interest, second way, is it proper in this case? I ask this, because in code we use jquery selector and for jquery selector, write clear javascript: this is justified and will it work always? or may be better use jquery $(this) for jquery selector? or not differnce?
this.idwill give you the internal DOM element property while$(this).attr("id")returns the value of ‘id’ attribute.The alternative to
this.idin jQuery is usingprop()method:$(this).prop("id"). However, using purethis.idconstruction will be easier and faster.