SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get all data- attributes

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

    So:
    Code:
    <body>
        <div class="section" data-test="123">
            <div class="top">
    
            </div>
            <div class="bottom" data-test="123">
    
            </div>
        </div>
        <div class="section">
            <p data-test="123">
    
            </p>
        </div>
    </body>
    ...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.

    Thanks.

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Simply use the following

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

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

  3. #3
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect. Thanks. Easy when you know how.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •