Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-09-2012, 02:20 AM   PM User | #1
johnsmith153
New Coder

 
Join Date: Mar 2012
Posts: 81
Thanks: 7
Thanked 0 Times in 0 Posts
johnsmith153 is infamous around these parts
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?
johnsmith153 is offline   Reply With Quote
Old 10-09-2012, 02:36 AM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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..
xelawho is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:44 PM.


Advertisement
Log in to turn off these ads.