ptamzz
ptamzz

Reputation: 9365

how to insert DOM element just after the div clicked

I've my mark up as

<div id="wrap">
   <div class="clickhere"></div>
   <div class="clickhere"></div>
   <div class="clickhere"></div>
   <div class="clickhere"></div>
   <div class="clickhere"></div>
</div>

Now, I want to add another <div class="clickhere"></div> just after the div clicked using jQuery. I thought of .append() or .prepend() but it only adds at the last or the first of the parent element (if applied at the parent element).

$('.clickhere').click(function(){
   // add <div class="clickhere"></div> just below $(this)
})

Upvotes: 0

Views: 1536

Answers (2)

RobG
RobG

Reputation: 147383

And in something approaching POJS:

> $('.clickhere').click(function(){

  var div = document.createElement('div');
  div.className = 'clickhere';
  this.parentNode.insertBefore(div, this.nextSibling);

> just below $(this) })

Upvotes: 0

n0nick
n0nick

Reputation: 1145

You have .insertAfter() for that :)

$('<div class="clickhere"></div>').insertAfter(this);

Upvotes: 5

Related Questions