0

I know it's a base problem, but I wasn't able to find anything fitting my situation.

I have a jQuery Mobile page with a list of elements. The elements references to a second pages which has a content that depends on what element where choose (for example a list of articles and choosing an article it should show the relative text). Basically I have to pass a parameter between pages.

I tried to do that using AJAX:

$.ajax({
    url : "index.html#page2",
    data : "id="+id,    
    dataType : 'json'
    type: "GET"
});

But i wasn't able to get the results in the second page.

How can I do that?

Matt07
  • 504
  • 7
  • 21

1 Answers1

1

By the look of your URL: index.html#page2. You are trying to navigate to an internal page (one already in the DOM). If this is the case, then make the logic in JavaSCript on #page2 and when you link to #page2, save the id value in a variable that the JavaScript on #page2 can access.

Something like:

<ul data-role="listview" id="myList">
    <li>
        <a href="#page2" data-id="987">This is some fake text... Click for More</a>
    </li>
</ul>
<script>
$(document).delegate('#page1', 'pageinit', function () {

    //bind to click event for all links in the `#myList` UL
    $(this).find('#myList').find('a').bind('click', function () {

        //save the ID of this list-item to a global variable so it can be used later
        window.myId = $(this).attr('data-id');
    });
});
$(document).delegate('#page2', 'pageshow', function () {

    //get the ID saved as a global variable
    var currentId = window.myId;

    //now do logic for second page
});
</script>

Here is a demo: http://jsfiddle.net/jasper/wFdet/

Jasper
  • 75,717
  • 14
  • 151
  • 146
  • Ok i got the idea but it doesn't work. I changed pageinit to 'pageinit' and now is better, but I think the redirect part is missing. When I click on the link it does nothing. – Matt07 Jul 02 '12 at 23:15
  • the second page part is working. But the first page one it's not being execute. – Matt07 Jul 03 '12 at 09:32
  • I updated my answer, I forgot to add the `data-url` of the second page to the `href` attribute of the links. – Jasper Jul 03 '12 at 16:56