Change tab programmatically on JQuery Mobile

jquery, jquery-mobile, jquery-ui

Solution

Working example: http://jsfiddle.net/Gajotres/aX7L9/

HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css" /> 
        <!--<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>-->
        <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>    
    </head>
    <body>     
        <div data-role="page" id="index" data-theme="a" >
            <div data-role="tabs" class="tabs">
                <div data-role="navbar">
                    <ul>
                        <li><a href="#one" data-theme="a">one</a></li>
                        <li><a href="#two" data-theme="a">two</a></li>
                        <li><a href="ajax-content.html" data-theme="a" data-ajax="false">three</a></li>
                    </ul>
                </div>
                <div id="one" class="ui-content">
                    <h1>First tab contents</h1>
                </div>
                <div id="two" class="ui-content">
                    <ul data-role="listview">
                        <li><a href="#">Acura</a></li>
                        <li><a href="#">Audi</a></li>
                        <li><a href="#">BMW</a></li>
                        <li><a href="#">Cadillac</a></li>
                        <li><a href="#">Ferrari</a></li>
                    </ul>
                </div>
            </div>
        </div> 
    </body>
</html>   

JavaScript:

$(document).on('pagecontainershow', function () {
    pageId = $('body').pagecontainer('getActivePage').prop('id'); 

    if (pageId === 'index') {
        setTimeout(function(){
            $(".tabs").tabs( "option", "active", 1 );
        }, 1000);
    }
});

jQuery Mobile uses jQuery UI tab widget so find everything you need here.

Problem

I have a navbar (bellow) and I would like to change tabs using Javascript, found no documentation to help me and a lot of broken code online, I would appreciate some help here. Test case: the tab has page-1 initially selected, I would like to change to page-2 using javascript. ``` <div data-role="tabs" id="tabs"> <div data-role="navbar" id="navbar"> <ul> <li><a href="#page-1" data-ajax="false" class="ui-btn-active">1</a></li> <li><a href="#page-2" data-ajax="false">2</a></li> <li><a href="#page-3" data-ajax="false">3</a></li> <li><a href="#page-4" data-ajax="false">4</a></li> <li><a href="#page-5" data-ajax="false">5</a></li> </ul> </div> ``` Tested Code: ``` var index = $('#tabs a[href="#page-2"]').parent().index(); $('#tabs').tabs('select', index); ``` Throws: ``` Error: no such method 'select' for tabs widget instance ``` After the solution I came up with these functions: ``` function changeNavPagePrevious(tabControl) { if ($("#" + tabControl + " .ui-tabs-active").prev().length > 0) { if ($("#" + tabControl + " .ui-tabs-active").prev().children().length > 0) { changeNavPage(tabControl, $("#" + tabControl + " .ui-tabs-active").prev().children().eq(0).attr("href").replace("#", "")); } } } function changeNavPageNext(tabControl) { if ($("#" + tabControl + " .ui-tabs-active").next().length > 0) { if ($("#" + tabControl + " .ui-tabs-active").next().children().length > 0) { changeNavPage(tabControl, $("#" + tabControl + " .ui-tabs-active").next().children().eq(0).attr("href").replace("#", "")); } } } function changeNavPage(tabControl, newPage) { $("#" + tabControl + " .ui-btn-active").eq(0).removeClass("ui-btn-active").removeClass('ui-tabs-active').removeClass("ui-state-active"); $('#tabs a[href="#' + newPage + '"]').addClass('ui-btn-active'); var index = $('#' + tabControl + ' a[href="#' + newPage + '"]').parent().index(); $("#" + tabControl).tabs("option", "active", index); ``` }

Original source