2

I am trying out jquery slidetoggle but I'm having issues trying to run it. Please check on the fiddle provided. https://jsfiddle.net/2yL2Q/63/

When I click on the flip class, its not doing anything.

Code Sample

<div class="some-div">
<div class="flip">Click to slide the panel down or up</div>
</div>
<div class="panel">Hello world!</div>

<div class="some-div">
<div class="flip">Click to slide the panel down or up</div>
</div>
<div class="panel">Hello world!</div>

<div class="some-div">
<div class="flip">Click to slide the panel down or up</div>
</div>
<div class="panel">Hello world!</div>

Script

$(document).ready(function () {
    $(".some-div .flip").click(function () {
        $(this).next('.panel').slideToggle("slow");
    });
});

1 Answer 1

3

You are trying to select a .panel element that is a following adjacent sibling to the clicked .flip element. Your markup would need to look like this in order for your jQuery to work:

<div class="flip">Click to slide the panel down or up</div>
<div class="panel">Hello world!</div>

Since those elements aren't siblings in the HTML you provided, you need to traverse the DOM and select the parent element. Simply use the .parent() method:

Updated Example

$(".some-div .flip").click(function () {
    $(this).parent().next('.panel').slideToggle("slow");
});

Alternatively, if the depth/nesting of the elements varies, you could also use the .closest() method:

Updated Example

$(".some-div .flip").click(function () {
    $(this).closest('.some-div').next('.panel').slideToggle("slow");
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Got it. May i know where can I find this doc?
@DroidBeginner Yep--I just linked to both of the mentioned methods in the answer. You can look the the jQuery API documentation to see all the methods - api.jquery.com

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.