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
The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.
To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class.
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.
Use document.querySelector on to select a div and then select an element within it with the given class after that. We just call querySelector on the element with the ID mydiv to select items inside that div. Therefore, innerDiv is the div with the class myclass .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With