0

So I am trying to make a section that folds up when the title is clicked on. It works if I use the following but obviously folds all sections up instead of just the clicked on:

$(".foldUpSection").find(".header").click(function()
{
    $(".foldUpSection").find(".foldMeUp").slideToggle();
});

I switched it to

$(".foldUpSection").find(".header").click(function()
{
    $(this).find(".foldMeUp").slideToggle();
});

but this does nothing. Am I missing something to why it is not passing the this on click?

Here is the HTML

<div class="foldUpSection">
    <span class="header">Unprocessed EDIs</span>
    <div id="unprocessedEdi" class="foldMeUp">
    </div>
</div>

3 Answers 3

4

In that context this refers to $(".foldUpSection").find(".header")

the find() function looks for descendants of a selector and $(".foldUpSection").find(".header") has no descendants with a class of foldMeUp.

Sign up to request clarification or add additional context in comments.

2 Comments

This is not this, easy. That was not this either.
Ah! Excellent answer! Now I understand my issue, I was at the same level as the object I was looking for, not over it! That makes sense, appreciate it.
1

If the DOM structure is not set in stone (so that you might have other elements between .header & .foldMeUp, use this:

$(".foldUpSection").find(".header").click(function()
{
    $(this).closest(".foldUpSection").find(".foldMeUp").slideToggle();
});

Here's the fiddle: http://jsfiddle.net/jEgK4/

Comments

1

Using $(this) and .find() made you target the wrong element.

Use .next() or .siblings(), find() searches for children, foldmeup is a sibling

   $(this).siblings(".foldMeUp").slideToggle();

or

 $(this).next(".foldMeUp").slideToggle();

DEMO

Documentation for .find()

2 Comments

Ah sweet, any reason why find doesn't work in a passed DOM object?
it would works if you use find from .foldUpSection because .foldMeUp is a child of that element. .find() is a lot like .children()

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.