Search code examples
domjquery-mobileprepend

jquery mobile appending/prepending text


I would like to append some code to a page using jQuery/jQuery mobile, but I am tripping up on some of the page() and pageshow and refresh methods.
Here is my code. you can cut, paste and run as is.

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Page Title</title>   
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.css" />
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.js"></script>

    <script>
    //$(document).ready(function()                  // get error when I use this
    $('div').live('pageshow', function () {  
        function fnCreateGroups(){
            var section = "<p>and add a whole bunch of html code...</p>";
             myClone = $(section);  
             alert((myClone).html());  // this works
             myClone.appendTo("#placeholder").page(); // but not appending          
        }
        fnCreateGroups();
    });         
    </script>

</head> 
<body> 
    <div data-role="page">
        <div id="placeholder">
            <div data-role="content">
                <div id="placeholder">this is a small amount of html code.</div>
            </div>
        </div>      
    </div>
</body>
</html>

Solution

  • Beta2 release notes:

    instead of:

    myClone.appendTo("#placeholder").page();
    

    try

    myClone.appendTo("#placeholder").trigger('create');
    

    From your comment:

    You are running the live() on all div's

    $('div').live('pageshow', function () 
    

    try using the page id instead

    $('#change_this_page_only').live('pageshow', function () 
    

    and this:

    <div data-role="page">
    

    to something like this:

    <div data-role="page" id="change_this_page_only">