Get all data- attributes

How do I get all the occasions of <div data-test=“value”> on a page?


    <div class="section" data-test="123">
        <div class="top">

        <div class="bottom" data-test="123">

    <div class="section">
        <p data-test="123">


…returns reference to the three occasions above and allows me to perform actions on the node they sit on.

I know $(“.section”).data(“test”) will get it but I want to get it for all occasions on any page with any HTML and any classes.

I tried $(‘body’).data(‘test’) and $(‘div’).data(‘test’) but nothing.


Simply use the following

var elements = $('[data-test]');

That will find all elements in the DOM that have an attribute called data-test.

Perfect. Thanks. Easy when you know how.