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

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-08-2012, 07:37 PM   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
Changing HTML with jQuery

I think the title makes sense:

I want to change this:
Code:
<div class="class1 x">
CONTENT A
</div>

<div class="class2 x">
CONTENT B
</div>

<div class="class3 x">
CONTENT C
</div>

<div class="class4">
CONTENT D
</div>

<div class="class5">
CONTENT E
</div>
...to:

Code:
<div class="class3 x">

  <div class="changed">
  CONTENT A
  </div>

  <div class="changed">
  CONTENT B
  </div>

  <div class="changed">
  CONTENT C
  </div>

</div>

<div class="class4">
CONTENT D
</div>

<div class="class5">
CONTENT E
</div>
So basically the 'x' groups all the content together and merges with the last class holding the 'x'.

I want to loop through all divs just incase the 'x' classes are not together.

I always struggle with manipulating HTML with jQuery.

Last edited by johnsmith153; 10-08-2012 at 07:49 PM..
johnsmith153 is offline   Reply With Quote
Old 10-08-2012, 07:51 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 945
Thanks: 7
Thanked 97 Times in 97 Posts
WolfShade is an unknown quantity at this point
You don't have to loop with jQuery. jQuery can make an array (or array-like object) based upon class name. Then you can use removeClass and addClass to alter the classes.

ie
Code:
$('.x')
will get you all elements that have 'x' as a class.

Code:
$('div.x')
will get you all DIVs that have 'x' as a class.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

Last edited by WolfShade; 10-08-2012 at 07:53 PM..
WolfShade is offline   Reply With Quote
Old 10-08-2012, 08:51 PM   PM User | #3
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
something like this, I guess...
Code:
<script>
$('div[class*=" x"]').not($('div[class*=" x"]:last')).each(function() {
$(this).attr("class","changed")
$('div[class*=" x"]:last').append($(this));
});
</script>
xelawho is offline   Reply With Quote
Old 10-08-2012, 09:04 PM   PM User | #4
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
actually more like this, strictly following the required outcome...

Code:
<script>
$($('div[class*=" x"]').not($('div[class*=" x"]:last')).get().reverse()).each(function() {
$(this).attr("class","changed")
$('div[class*=" x"]:last').prepend($(this));
});
</script>
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
johnsmith153 (10-08-2012)
Old 10-08-2012, 09:15 PM   PM User | #5
johnsmith153
New Coder

 
Join Date: Mar 2012
Posts: 81
Thanks: 7
Thanked 0 Times in 0 Posts
johnsmith153 is infamous around these parts
Thanks, I really appreciate this. I'll need to look at what each part does as it looks pretty complicated.

I've actually slightly changed my needs on this, and a different post is here: http://www.codingforums.com/showthread.php?t=275781

I'm guessing someone will complain about double posting, but they are supposed to be different. (Sorry)

I'll try and work out how to change it to work for that, but any pointers would help.

Thanks again.
johnsmith153 is offline   Reply With Quote
Old 10-08-2012, 10:27 PM   PM User | #6
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,592
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Quote:
Originally Posted by johnsmith153 View Post
I'm guessing someone will complain about double posting […]
The only thing I’m complaining right now is that you keep posting your questions in the wrong forum. jQuery is a JavaScript framework/library and therefore belongs into the JS frameworks subforum to which I’ve moved this thread now.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Users who have thanked VIPStephan for this post:
johnsmith153 (10-09-2012)
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 11:14 PM.


Advertisement
Log in to turn off these ads.