Reputation: 4187
I would like to append HTML into a div of a particular ID there .How do i select it ?
<div id="1">
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
</div>
<div id="2">
<div class="c"></div>
<div class="c"> TO APPEND INSIDE THIS DIV </div>
<div class="c"></div>
</div>
<div id="3">
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
</div>
i was using
$(data).appendTo("#"+id).find(".c");
but its giving me problem as the appending is not going inside the div but outside the div
Upvotes: 8
Views: 20419
Reputation: 2881
you can try,
$('div#3 div.c').eq(1).append('<div class="new">NEW</div>');
or
$('<div class="new">NEW</div>').appendTo($('div#4 .c:eq(1)'));
You can try:
$(data).appendTo($('#'+ id +' .c'));
For more specificity may use index of any specific div.c
;
$(data).appendTo($('#'+ id +' .c:eq(2)')); // append to third `div.c`
UPDATE: for appendTo()
just wrapped the target selector with $()
and it works for me.
NOTE DON'T USE ONLY NUMERIC ID. IT ID PROHIBITED. FOR MORE READ
Upvotes: 9
Reputation: 6500
This work.
$('#2 .c:eq(1)').html("<p>Hello</p>");
Edit:
$('<p>Hello!</p>').appendTo($('#2 .c:eq(1)'));
Upvotes: 1
Reputation: 39689
ID attributes cannot start with a number. If you change your id attribute to something like item-2
, then you can use the following:
var html = '<div></div>';
$('#item-2 .c:eq(1)').append(html);
Upvotes: 2