Selecting Elements by Data Attribute in jQuery

Published on October 13, 2015 by

Selecting elements by data attributes is very easy in jQuery. As with all other attributes, we can simply use the attribute selector. In fact, we do not need to handle the data attribute in any special way, as long as the data attribute is available in the DOM. Please consider the following example.

var matches = $('.news-item[data-news-id="52"]');

In the above example, we select all elements with the class news-item that also have an attribute named data-news-id with a value of 52.

That is really all there is to it.

Author avatar
Bo Andersen

About the Author

I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!

Leave a Reply

Your e-mail address will not be published.