Reputation: 10525
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
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
Reputation: 6201
You can turn it around
$('#test div').append($('<h1>hi</h1>'));
Upvotes: 0
Reputation: 57105
This will do it
:lt documentation
div:lt(2)
will select first two elements
$('<h1>hi</h1>').appendTo('#test div:lt(2)');
Upvotes: 5