Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    81
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Get all data- attributes

    Is there a way (jQuery is fine, but not essential) to get all the data- attributes on a page and then change the values in the element's class to those in the data- attribute?
    E.g.

    this:
    Code:
    <div class="class1 class 2">
    A
    </div>
    <div class="class1 class 2" data-class-override="class3 class 4">
    B
    </div>
    <div class="class1 class 2">
    C
    </div>
    ...becomes:

    Code:
    <div class="class1 class 2">
    A
    </div>
    <div class="class3 class 4">
    B
    </div>
    <div class="class1 class 2">
    C
    </div>
    I need it to search the entire page as they could be anywhere.

    I know $(this).data("class-override") would get it but how search the whole page and how do it in the best way?

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    I don't think you're paying attention to the answers you're being given, or thinking about how they work

    Code:
    <script>
    $('div').each(function() {
    $(this).attr('class',$(this).attr("data-class-override")).attr("data-class-override",null)
    })
    </script>
    Last edited by xelawho; 10-09-2012 at 02:38 AM.


  •  

    Posting Permissions

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