Categories
JavaScript Answers

How to get custom attribute values with jQuery?

Spread the love

Sometimes, we want to get custom attribute values with jQuery.

In this article, we’ll look at how to get custom attribute values with jQuery.

How to get custom attribute values with jQuery?

To get custom attribute values with jQuery, we can use the data method.

For instance, we write:

<div data-something="bar"></div> 

to add a div with the data-something custom attribute.

And then we get the value of data-something by writing:

console.log($("div").data("something"))

We select the div with $.

And then we call data with the part of the attribute name after data- to get its value.

Therefore, the console log logs 'bar' as a result.

Conclusion

To get custom attribute values with jQuery, we can use the data method.

Leave a Reply

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