Question

I know that it's possible to create a JQuery Mobile page dynamically by writing out a long string and appending it to the page container. However I want to create the page as an object and append it to the page container.

This is how I used to create the pages: http://jsfiddle.net/pjUcn/

var page = $("<div data-role='page' id='page'><div data-role=header>
<a data-iconpos='left' data-icon='back' href='#' data-role='button' 
data-rel='back'>Back</a><h1>Dynamic Page</h1></div>
<div data-role=content>Stuff here</div></div>");

page.appendTo($.mobile.pageContainer);

$.mobile.changePage('#page');

This is how I want to create them: http://jsfiddle.net/8NZMw/2/

var page = $('<div/>'),
     header = $('<div/>'),
     back = $('<a/>'),
     title = $('<h1/>'),
     content = $('<div/>');

page.data('role', 'page');
page.attr('id', 'page');

header.data('role', 'header');

back.data('iconpos', 'left');
back.data('icon', 'back');
back.data('role', 'button');
back.data('rel', 'back');
back.attr('href', '#');
back.text('Back');

title.text('Dynamic Page');

header.append(back);
header.append(title);

page.append(header);

content.data('role', 'content');
content.text('stuff here');

page.append(content);

page.appendTo($.mobile.pageContainer);

$.mobile.changePage('#page');

I have no problem creating other simple JQuery Mobile elements, like this, I just can't figure out how to create a whole page this way. I even tried calling a .trigger("create") on the page object, but that still didn't do the trick.

Any help would be greatly appreciated, thanks!

Was it helpful?

Solution

Here ya go, this works for me (notice where I commented out your code to make it work) -

var page = $('<div/>'),
    header = $('<div/>'),
    back = $('<a/>'),
    title = $('<h1/>'),
    content = $('<div/>');

//page.data('role', 'page');
page.attr('data-role', 'page');
page.attr('id', 'page');

//header.data('role', 'header');
header.attr('data-role', 'header');

back.data('iconpos', 'left');
back.data('icon', 'back');
back.data('role', 'button');
back.data('rel', 'back');
back.attr('href', '#');
back.text('Back');

title.text('Dynamic Page');

header.append(back);
header.append(title);

page.append(header);

//content.data('role', 'content');
content.attr('data-role', 'content');
content.text('stuff here');

page.append(content);

page.appendTo($.mobile.pageContainer);

$.mobile.changePage('#page');

Here is a jsFiddle demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top