0

I want to active an accordion tab on button click with activate() method https://devdocs.magento.com/guides/v2.1/javascript-dev-guide/widgets/widget_tabs.html#fedg_tabs_methods-activate

This is my test code

<div id="element">
        <div class="custom_collapsible">
            <div class="custom_trigger">
                <span>Title 1</span>
            </div>
        </div>
        <div class="custom_content">Content 1</div>

        <div class="custom_collapsible">
            <div class="custom_trigger">
                <span>Title 2</span>
            </div>
        </div>
        <div class="custom_content">Content 2</div>

        <div class="custom_collapsible">
            <div class="custom_trigger">
                <span>Title 3</span>
            </div>
        </div>
        <div class="custom_content">Content 3</div>
    </div>

    <button id="btn">Activate 1</button>
    <script>

        if (2 == 2) {
            var collapsible = true
        } else {
            var collapsible = false
        }

        require([
            'jquery',
            'accordion'], function ($) {
            $('#btn').click(function() {
                $("#element").accordion().activate(2);
            });
            $("#element").accordion({
                active: [0,1],
                closedState: "accordion-closed",
                collapsible : collapsible, 
                collapsibleElement : ".custom_collapsible", 
                content : ".custom_content",
                disabled : [2],
                disabledState : "accordion-disabled",
                trigger : ".custom_trigger",
                icons: {"header": "plus", "activeHeader": "minus" }

            });
        });
    </script>

but i get this error

Uncaught TypeError: $(...).accordion(...).activate is not a function

what i'm wrong?

2 Answers 2

1

This is right way

$('#btn').click(function() {
    $("#element").accordion("activate", [1]);
});
1
0

Give your trigger a unique class or id e.g

<div class="custom_collapsible">
        <div class="custom_trigger unique-class">
            <span>Title 1</span>
        </div>
</div>

then in the script, you can click the trigger

$('.unique-class').click();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.