Joe.wang
Joe.wang

Reputation: 11791

How to select direct child with specified class name in a context

All, Forgive me I am not professional of the jquery, I am stuck with figure out the selector as the title show . please help me thanks.

I have a function like this:

function (container)
{
   var selectContentDiv= $("....",container);//I just don't know how to working with the selector context.
}

Supposed we the container which contains the html structure like the below:

    <div class="block-head">
       <h3>test</h3>
    </div>
    <div class="block-content"><!--I want to get this one-->
       <div class="block-head">
          <h3>test</h3>
       </div>
       <div class="block-content">
          ....
       </div>
    </div>

I want to know how to get the direct first child which have the class named block-content of the context? thanks.

Upvotes: 3

Views: 2616

Answers (5)

Anujith
Anujith

Reputation: 9370

Use:

$('div > block-content')

Upvotes: 1

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

<div class="block-content"><!--I want to get this one-->
       <div class="block-head">
          <h3>test</h3>
       </div>
       <div class="block-content">
          ....
       </div>
       <div class="block-head">
          <h3>test</h3>
       </div>
    </div>

from above HTML if you want to find direct child of block-content

$('.block-content > div.block-head')

EDIT according to Dream Eater Comment

Above code is added for example where .block-content is the parent and div.block-head is the child element.

So complete syntax would be.

$('parent > child')

Upvotes: 3

Prasanth Bendra
Prasanth Bendra

Reputation: 32750

Try this :

$(".block-head").next(".block-content")

This will give you the first .block-content after .block-head

Upvotes: 0

To get the first child of an element which has a .block-content class, use this:

$('div > .block-content');

I use a div as your parent element, which you didn't include in your html.

Upvotes: 3

Wim Ombelets
Wim Ombelets

Reputation: 5265

this will get you the first occurrence of a div element with class block-content:

$('div.block-content').first()

Upvotes: 0

Related Questions