9

I want to select the second <p> tag and style it within a itemize class div. Here is the 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 style the first <p> which is immediately after the second <div>. The second <p> has no ID or class.

How can I select it via jQuery?

2
  • thanks this works but i have an other issue now.. :S. basicall the structure is the same but js file include another P before it select some thing then its change to <p><strong> Packages</strong></p>. how can i do that. like first p coming from js <p>select your order</p> when i select some thing it changes via jquery to <p><strong>Packages:</strong> </p> <!-- i want to select this P tag-->. how can i add class to the second appeared P Commented Jun 18, 2012 at 7:31
  • In future, please spend more time writing or formatting your post. Formatting help can be found here Commented Jun 18, 2012 at 10:24

7 Answers 7

15
$('.itemize div p:first').html()

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

If you want to add class to that p tag:

$('.itemize div p:first').addClass('selected');
Sign up to request clarification or add additional context in comments.

3 Comments

Links are not answers. At least copypasta your line of JS in the answer as well. Taken from meta: Link-only answers can be actively harmful if links break. And they do break, even on sites that are seemingly going to be around forever.. Thanks for the update. :)
thanks this works but i have an other issue now.. :S. basicall the structure is the same but js file include another P before it select some thing then its change to <p><strong> Packages</strong></p>. how can i do that. like first p coming from js <p>select your order</p> when i select some thing it changes via jquery to <p><strong>Packages:</strong> </p> <!-- i want to select this P tag-->.
Do you just want to add class to first p tag?? If yes then i have updated code here. jsfiddle.net/QJTYx/4
8

You can do this way:

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

.itemize > div goes till:

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

The > allows to target direct children whereas eq(index) is used to get first p that you want.

Comments

6
var test = $('.itemize')​.find('div:first')​.find(​'p:first')​​​.html();
alert(test);
​

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

Comments

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

Comments

2

This should do the trick

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

Comments

2

You can try this..

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

hope it will works..

Comments

1

$('.itemize>div>p').first().css(styles go here) most of the ones above work as well

jQuery selectors work a bit like css selectors, check out this tutorial to get more info.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.