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?
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.
$('.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'); var test = $('.itemize').find('div:first').find('p:first').html(); alert(test); Try it here: http://jsfiddle.net/arvind07/H8vwA/