Choosing a <p> in a div using jQuery

I want to select the second <p> and create it in the itemize class. Here is an example HTML:

 <div class="itemize"> <p> Order Summery</p> <div> <p><strong>Packages:</strong> </p> <!-- i want to select this P tag--> <p><strong>Date:</strong> </p> <p><strong>Style:</strong> </p> </div> </div> 

I want to select and create the first <p> , which is immediately after the second <div> . The second <p> has no identifier or class.

How can I select it through jQuery?

+6
source share
7 answers

You can do this:

 $('.itemize > div > p:eq(0)') 

.itemize > div goes to:

 <div class="itemize"> <p> Order Summery</p> </div> 

AND

.itemize > div > p:eq(0)

 <div class="itemize"> <p> Order Summery</p> <div> <p><strong>Packages:</strong> </p> </div> </div> 

> allows you to orient direct children, while eq(index) used to get the first p you want.

+5
source
 $('.itemize div p:first').html() 

Check out this link: http://jsfiddle.net/QJTYx/

If you want to add a class to this p tag:

 $('.itemize div p:first').addClass('selected'); 
+12
source
 var test = $('.itemize')​.find('div:first')​.find(​'p:first')​​​.html(); alert(test);​ 

Try it here: http://jsfiddle.net/arvind07/H8vwA/

+3
source
 $('.itemize>div>p:first').addClass('someClass'); 
+2
source

That should do the trick

 $('.itemize div p').first().addClass('hello'); 
+1
source

$('.itemize>div>p').first().css(styles go here) most of the above works also work

JQuery selectors work a bit like a css selector, see the tutorial for more information.

+1
source

You can try this.

 $(".itemize div p:first").text(); 

hope it works.

+1
source

Source: https://habr.com/ru/post/918328/


All Articles