0

I am trying to create an accordion inside of an accordion... and I am struggling a little.

essentially, I have a div .applicant, which upon click adds a class .expand, which sets the height to auto, but, inside of the .applicant div, I have another div .applicant-child, which SHOULD do the same thing, and does... but, .applicant closes when you click .applicant-child, meaning you have to click the .applicant again to open view the nested element.

Here is my code:

HTML

    <div class="row">
        <div class="col-sm-12">
            <div class="applicant">
                <p><b>PS4 Tournaments</b></p>
                <div class="applicant-child">
                    <p>lalal</p>
                    <p>lalal</p>
                </div>
            </div>
        </div>
    </div>

jQuery

    $('.applicant').click(function(){
        if ($(this).hasClass('expand')) {
            $(this).removeClass('expand');
        } else {
            $( this ).addClass('expand');
        }
    });

    $('.applicant-child').click(function(){
        if ($(this).hasClass('expand')) {
            $(this).removeClass('expand');
        } else {
            $( this ).addClass('expand');
        }
    });

I could simply remove $(this).removeClass('expand'); from .appliant, but we'll be displaying a lot of data, so that isn't ideal.

How do I solve this?

Thanks in advance :)

3
  • make application div and application-child div siblings. Nested clicks are bad idea Commented Aug 9, 2018 at 15:23
  • You need to call stopPropagation() on the click event that's raised. You can also simplify the class adding/removnig by just using toggleClass() Commented Aug 9, 2018 at 15:24
  • Cheers, @RoryMcCrossan. Wasn't aware of that. thank you! Commented Aug 9, 2018 at 15:39

3 Answers 3

1

That's just event bubbling an expected behaviour. See this link on jQuery on how to disable the click-Event to bubble up the DOM and triggering the event on your parent element.

Basically, you just have to do this:

$('.applicant-child').click(function(event){
    event.stopPropagation();
    if ($(this).hasClass('expand')) {
        $(this).removeClass('expand');
    } else {
        $( this ).addClass('expand');
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! I wasn't aware of this, I am somewhat new to jQuery. Thank you very much, Agash! Really appreciate it :)
0

You want to prevent bubbling. Bubbling means, that the event you are reacting to is being passed up the DOM to the parent objects, until it reaches the window.

Check out the "event.stopPropagation()" method, which will prevent any subsequent listeners from reacting.

1 Comment

Thank you, Treffnon! exactly what I am after.
0

On your click handler if you pass through a param:

$('.applicant').click(function(event){
    console.log(event.target);
    if ($(this).hasClass('expand')) {
        $(this).removeClass('expand');
    } else {
        $( this ).addClass('expand');
    }
});

you can use event.target to check if you are clicking on the parent or the child and decide on what action to take from there.

1 Comment

Thanks, Ryan. I have opted to go with the answer @agash has provided, but it's great to know this can be done in multiple ways. Thanks!

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.