jQM - Send parameters from paga 1 to page 2

by Dragan Gaić

HTML

<link rel="stylesheet" href="http://jeromeetienne.github.com/jquery-mobile-960/css/jquery-mobile-fluid960.css">
<script src="http://timeago.yarp.com/jquery.timeago.js"></script>
<script src="http://imakewebthings.github.com/jquery-waypoints/waypoints.min.js"></script>
<!DOCTYPE html>
<html>
<head>
    <title>jQM Complex Demo</title>
    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0"/>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>    
</head>
<body>
    <div data-role="page" id="index">
        <div data-theme="a" data-role="header">
            <h3>
                First Page
            </h3>
            <a href="#second" class="ui-btn-right">Next</a>
        </div>

        <div data-role="content">
            <ul data-role="listview" data-theme="a" id="test-listview">
                <li><a href="#" id="1">Link 1</a></li>
                <li><a href="#" id="2">Link 2</a></li>
                <li><a href="#" id="3">Link 3</a></li>
                <li><a href="#" id="4">Link 4</a></li>
                <li><a href="#" id="5">Link 5</a></li>
            </ul>    
        </div>

        <div data-theme="a" data-role="footer" data-position="fixed">

        </div>
    </div> 
    <div data-role="page" id="second">
        <div data-theme="a" data-role="header">
            <h3>
                Second Page
            </h3>
            <a href="#index" class="ui-btn-left">Back</a>
        </div>

        <div data-role="content">

        </div>

        <div data-theme="a" data-role="footer" data-position="fixed">

        </div>
    </div>    
</body>
</html>

JavaScript

$(document).on('pagebeforeshow', '#index', function(){       
    $('#test-listview li a').each(function(){
        var elementID = $(this).attr('id');      
        $(document).on('click', '#'+elementID, function(event){  
            if(event.handled !== true) // This will prevent event triggering more then once
            {
                listObject.itemID = elementID; // Save li id into an object, localstorage can also be used, find more about it here: http://stackoverflow.com/questions/14468659/jquery-mobile-document-ready-vs-page-events
                $.mobile.changePage( "#second", { transition: "slide"} );
                event.handled = true;
            }              
        });
    });
});

$(document).on('pagebeforeshow', '#second', function(){       
    $('#second [data-role="content"]').html('You have selected Link' + listObject.itemID);
});

var listObject = {
    itemID : null
}