0

Attempting to create an accordion, but I can't figure out to check if the wrapper has the active class.

My jQuery:

$('.faq_wrap span.plus').on(touchClick, function (e) {
    var topEl = $(this).closest('.faq_wrap');
    if (topEl.hasClass('active')) {
        topEl.removeClass('active');
        topEl.find('.answer').slideUp('fast');
        return false;
    }
    topEl.each(function (index, el) {
        topEl.removeClass('active');
        topEl.find('.answer').slideUp('fast');
    });
    topEl.find('.answer').slideDown('fast');
    topEl.addClass('active');
    console.log(topEl);
});

Here's the output from console.log:

enter image description here

I'm trying to check whether .faq_wrap.active already exists. If it does, slide the accordion up before sliding the new one down. As of right now, this check is ignored and I can open as many accordions at once as I want. Thoughts?

10
  • 1
    touchClick is a variable or a string ? Commented Jul 24, 2014 at 20:27
  • 1
    What does your HTML look like? You're logging topEl.children, but checking topEl.hasClass. What happens if you console.log(topEl);? Commented Jul 24, 2014 at 20:28
  • 1
    If there can only ever be one .active piece, why not simply do if ($('.faq_wrap.active')) { ... rest of code? Wondering why you only check the closest .faq_wrap too. Commented Jul 24, 2014 at 20:30
  • @YuliamChandra touchClick is just a click variable I have set since I'm working with mobile devices. Commented Jul 24, 2014 at 20:33
  • 2
    Why not just use the jQuery UI Accordion? Commented Jul 24, 2014 at 20:42

2 Answers 2

1

Use .get()

topEl.get(0).className
Sign up to request clarification or add additional context in comments.

4 Comments

Can that be changed to getting the full div? Just so I can check if it has class active or not.
Sure, just use topEl.get(0)
okay, another question (sorry im dumb): why do i get undefined is not a function when i swap the following in, on my 2nd line: var topEl = $(this).closest('.faq_wrap').get(0);
Because then the element is no longer a jQuery object but a plain JavaScript DOM object, and on this you can't call methods like .hasClassor .each.
0

You have multiple collapsible/ expandable sections in your HTML, from which only one is ever going to be active (expanded). You use .closest() but if there's only one you could just as well use the direct class selector .faq_wrap.active (it will return either 0 or 1 element, depending on whether the user has already clicked). There's also no point in doing topEl.each because .closest() will return only the closest node (here, the parent). So the following should be better ( & more concise)

$('.faq_wrap span.plus').on(touchClick, function (e) {
    var topEl = $(this).closest('.faq_wrap'),
        activeEl = $('.faq_wrap.active');

    if (activeEl.length) { // implied: if it's bigger than 0
        activeEl.removeClass('active');
        activeEl.find('.answer').slideUp('fast');
    }
    topEl.find('.answer').slideDown('fast');
    topEl.addClass('active');
});

NB: Just wondering, why did you use the return false; statement?

Comments

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.