views:

51

answers:

3

I have one class of divs ('open') the contain another class of divs ('items') like this:

<div class="open">
    <div class="item">
    this is a test 1
    </div>

    <div class="item">
    this is a test 2
    </div>
</div>

What I'm looking to do is a slidedown of all the 'item' class divs that are in the 'open' class that's clicked.

So far I have

$(document).ready(function () {

    $('.open').click(function () {

        $(this).find(':first-child').slideDown('1000');

    });

but it's not working. Any suggestions?

Thanks, John

+3  A: 

Instead of :first-child you want immediate children, which you can get in this case using .children(), like this:

$('.open').click(function () {
  $(this).children().slideDown('1000');
});

:first-child is for getting the first child element at each level (and in each "branch"), .children() or the child selector (>) are for getting all immediate/"first level" children.

Nick Craver
Great minds.....
Marko
+2  A: 

How about

$(".open").click(function() {
    $(this).children().slideDown(1000);
});
Marko
We answered at the same time. I think mine is slightly more accurate since he really wants to call only the children with the ID of "open," and you can certainly see an edge case where there are children without that ID.
Josh Smith
@Josh - His children have a class not an ID, and it's `item`, not `open` :)
Nick Craver
Yeah, I'm all over the place today. Been dealing with terrible CI issues and am rather frazzled. Thanks for the catch.
Josh Smith
A: 

First, I assume that you're actually closing off the $(document).ready, since in the code block you posted, it's not closed. Make sure you're doing so in your code.

Second, aren't you just looking for children() of .open? In which case you want

$(document).ready(function() {
  $('.open').click(function() {
    $(this).children('.item').slideDown('1000');
  });
});

Edited: Thanks to comments, I've removed the live() suggestion.

Josh Smith
I think using live() in this case is completely unnecessary.
Marko
`.live()` isn't always *better*, so recommending it as a habit is a bad idea, it needs to be applied or not based on the situation. Also his children don't have an `.open` class, so your code would have no effect :)
Nick Craver
Whoops. I meant `.item`. I actually didn't know that about `live()`. I'll keep it in mind. Thanks!
Josh Smith
Edited to take your comments into account.
Josh Smith