jQuery Attributes Get the attribute value of a HTML element

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

When a single parameter is passed to the .attr() function it returns the value of passed attribute on the selected element.

Syntax:

$([selector]).attr([attribute name]);

Example:

HTML:

<a href="/home">Home</a>

jQuery:

$('a').attr('href');

Fetching data attributes:

jQuery offers .data() function in order to deal with data attributes. .data function returns the value of the data attribute on the selected element.

Syntax:

$([selector]).data([attribute name]);

Example:

Html:

<article data-column="3"></article>

jQuery:

$("article").data("column")

Note:

jQuery's data() method will give you access to data-* attributes, BUT, it clobbers the case of the attribute name. Reference



Got any jQuery Question?