JQuery Selector of a specific class within a specific identifier

I would like to add HTML in the div of a specific id there. How to choose 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 used

  $(data).appendTo("#"+id).find(".c"); 

but this gives me a problem, since the addition does not occur inside the div, but outside the div

+8
jquery
source share
4 answers

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 greater specificity, the index of any particular div.c can be used;

 $(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 DO NOT USE ONLY A NUMERIC ID. IT ID is FORBIDDEN. FOR MORE READ

+9
source share

Identity attributes cannot begin with a number. If you change your id attribute to something like item-2 , you can use the following:

 var html = '<div></div>'; $('#item-2 .c:eq(1)').append(html); 
+2
source share

This work.

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

Edit:

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

Fiddle

+1
source share
 $('#2 .c').html('<p>Stuff, junk, crud</p>'); 
0
source share

All Articles