Navin Rauniyar
Navin Rauniyar

Reputation: 10525

append same element into two places

I have the following html....

<div id="test">
    <div>
      <img /><img /><img />
    </div>
    <div>
      <img /><img /><img />
    </div>
</div>

jquery:

$('#clickto').click(function(){
   $('<h1>hi</h1>').appendTo('#test div:nth-child(1)');
   $('<h1>hi</h1>').appendTo('#test div:nth-child(2)');
});

The same element I have to append since I appended two times. I think there is an easy way.

I tried this but appends to only one div:

$('<h1>hi</h1>').appendTo('#test div');

Upvotes: 1

Views: 849

Answers (3)

Rajarshi Das
Rajarshi Das

Reputation: 12320

<div id="test">
<div class="select_me">
  <img /><img /><img />
</div>
<div class="select_me">
  <img /><img /><img />
</div>
</div>


 $('<h1>hi</h1>').appendTo('#test div.select_me');

Upvotes: 0

Jakub Kotrs
Jakub Kotrs

Reputation: 6201

You can turn it around

$('#test div').append($('<h1>hi</h1>'));

Upvotes: 0

Working DEMO

This will do it

:lt documentation

div:lt(2) will select first two elements

$('<h1>hi</h1>').appendTo('#test div:lt(2)');

Upvotes: 5

Related Questions