Dean Elliott
Dean Elliott

Reputation: 1533

Add div after every element with a particular class

Is there a way to automatically insert a div into my markup after every element with a particular class?

For example;

If I have the following markup

<div class="three-col"></div>

<div class="three-col"></div>

<div class="three-col last"></div>

Is there a way to add

<div class="after-last"></div>

Automatically after the "three-col last" div?

I'm creating a Wordpress theme and I've created some column shortcodes, but as the divs are floated left, if the last column has less content than the rest, any content added after it jumps up underneath it.

Any advice would be great.

Thanks.

Upvotes: 1

Views: 970

Answers (2)

jbatista
jbatista

Reputation: 1002

If I was you I didn't use the last class, instead of that you can do something like that:

<div class="three-col"></div>

<div class="three-col"></div>

<div class="three-col"></div>

$('div.three-col:last').after('<div class="after-last"></div>');

Upvotes: 2

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

Yes, Try .after method.

$('.three-col.last').after('<div class="after-last"></div>');

Upvotes: 6

Related Questions