Jquery Find first direct child of a type

I am looking for a way to find the first direct child of an element, of a precise type.

Let's imagine this markup:

<div id="mainDiv">
    <div id="otherDiv">
        <p> Stuff </p>
    </div>
    <p> Stuff 2 </p>
    <p> Stuff 3 </p>
</div>

So here, what I want to get is "Stuff 2" the first paragraph to be a direct child.

If using jquery I do something like $('#mainDiv').find('p:first'); I will get the paragraph inside the first div.

What I need is to ignore nested childs and take only the first direct one. How should I do that?

Answers


Use the direct descendant selector >

$('#mainDiv > p:first')

or even children()

$('#mainDiv').children('p').first()

Need Your Help

Applescript only executing half my code

excel applescript

I have two simple snips of code to script Excel, one gets the other sets.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.