How to know when Collapsible expands or closes?

Asked

Viewed 287 times

1

I have a Collapsible, and when it is expanded I want to add elements to it, and when it is collected I want to exclude the elements.
At the moment I’m using it as follows:

<div id="expansorTurma" data-role="collapsible">
    <h4 id="expansorTurmaTitle" onclick="montarCursos()">Não sei o código da minha turma.</h4>
    <h6>Escolha seu curso.</h6>

    <div id="cursos"></div>
</div>

<script>
    function montarCursos() {
         /** **/
        $("#cursos").append(<button class="ui-btn ui-corner-all">Elemento</button>')
        /** **/
    }
</script>

But in this case I find a big problem, because every time the button is clicked a new element is created, but I need it to be created only once, and then deleted.

  • 3

    When the element is closed it has the class ui-collapsible-collapsed, when opened this class is removed. To know when it is open or not, just see if the class mentioned above is present.

  • 1

    Thanks friend, it worked perfectly! I could post as reply...

1 answer

1


According to the description of the api https://api.jquerymobile.com/collapsible/#Event-Collapse

You can "listen" to these events:

$( "#expansorTurma" )
   .on( "collapsiblecollapse", function( event, ui ) {
         $("#cursos").html("");
    } )
   .on( "collapsibleexpand", function( event, ui ) {
         $("#cursos").append('<button class="ui-btn ui-corner-all">Elemento</button>');
    } );

Browser other questions tagged

You are not signed in. Login or sign up in order to post.