Search code examples
javascriptjqueryhtmljquery-mobile

Trigger the collapsible in JQUERY Mobile with a seperate button


I have a collapsible in JQUERY Mobile that i im using as a read more. when the collapsible gets expanded there is a fixed button and the bottom of the screen that says close to make it so you dont have to scroll back to the top to close the collapsible.

What i want to do is trigger the collapsible to close with the fixed close button on the bottom of the screen. i have tried bind methods but my inexperience is hindering my understanding.

here is the code im using.

<div id="showbutton" data-role="collapsible" data-theme="b" data-transition="turn" data-content-theme="d" class="ui-shadow ui-btn-inline">
    <h2>Read more</h2>
    <h2 id="content">Lots and lots of content here</h2>

        <footer align="center" id="closefooter" data-role="footer" data-position="fixed" data-theme="b">
            <script>
                $(function() {
                    $("#closebutton").click(function() {
                        console.log("ok");
                    $( "#showbutton" ).trigger( "closebutton" );
                    });
                 });
            </script> 
        <a id="closebutton">close</a>
    </footer>

</div>

Solution

  • Working example: http://jsfiddle.net/nmxav27t/

    $(document).on("click", "#closebutton", function(event) {
        $("#showbutton").collapsible("collapse");
    });
    

    You need to bind a click (or tap event) on the button you want to use for closing, then trigger collapse on the showbutton id.