8

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

Yahoo
  • 4,093
  • 17
  • 59
  • 85

4 Answers4

9

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

Community
  • 1
  • 1
The System Restart
  • 2,873
  • 19
  • 28
  • 1
    @AdiMathur I have shared a link in my answer, read that. the link is http://stackoverflow.com/questions/7987636/why-cant-i-have-a-numeric-value-as-the-id-of-an-element – The System Restart May 15 '12 at 16:51
  • 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 – Yahoo May 15 '12 at 16:55
  • Doesnt this mean ... Append to the ( ID which also has a class .c ) ? – Yahoo May 15 '12 at 17:06
  • 1
    @AdiMathur See my last update answer, you have to wrap the target with `$()` for `appendTo()`. – The System Restart May 15 '12 at 17:14
  • mine wasnt working because there awsnt a space in " .c" . Space is need ? it stated working after giving a space – Yahoo May 15 '12 at 17:40
  • @AdiMathur space is needed, because `.c` is child is of `#1`, `#2`, `#3` and Space target to child element – The System Restart May 16 '12 at 01:07
2

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);
jmar777
  • 38,796
  • 11
  • 66
  • 64
  • 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 – Yahoo May 15 '12 at 16:53
  • That honestly doesn't make a lot of sense. `.append()` will add the html to the end of any matching elements. In your case, you should have 3 matching elements, because you have 3 elements with class `c` (within the element with the specified id). Given that you only wanted to append to the second item, I recommended `.c:eq(1)`. So, `$(data).appendTo('#' + id).find('.c:eq(1)')` should work. Perhaps you can create a [jsfiddle](http://jsfiddle.net/) if that's still not working for you? – jmar777 May 15 '12 at 16:59
1

This work.

$('#2 .c:eq(1)').html("<p>Hello</p>");

Edit:

​$('<p>Hello!</p>').appendTo($('#2 .c:eq(1)'));​

FIDDLE

Andrea Turri
  • 6,480
  • 7
  • 37
  • 63
0
$('#2 .c').html('<p>Stuff, junk, crud</p>');
lbstr
  • 2,822
  • 18
  • 29