Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Selector of a particular class inside a particular id

Tags:

jquery

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

like image 551
Yahoo Avatar asked May 15 '12 16:05

Yahoo


People also ask

How do you select an element with ID and class?

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.

How do you select an element with a particular class selected?

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.

How do I target a specific element in jQuery?

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.

How do I select a class inside a div?

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 .


1 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 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

like image 145
The System Restart Avatar answered Oct 21 '22 04:10

The System Restart