Get an attribute value from object using javascript and jQuery

The attr() is a function of jQuery It’s not a native function of Javascript. Below shows how to get the same information using both jQuery and Javascript:

Javascript:

var src = document.getElementById('myImg').getAttribute('src');

The getAttribute() method of the element interface returns the value of a specified attribute on the element. If the given attribute does not exist, the value returned will either be null or "" (the empty string);

jQuery:

var src = $('#myImg').attr('src');

Leave a comment

Your email address will not be published. Required fields are marked *