Enhance dynamically generated HTML using jQuery Mobile

Using jQuery Mobile version 1.2.0

I am dynamically generating HTML using JavaScript ($(selector).html(content)), adding it to the DOM, and then displaying it ($.mobile.changePage()).

After that, I make an AJAX call, retrieve some data, and re-generate the HTML (keeping the parent element, $(selector), the same but changing its content with html(...)).
At this point, the HTML does not have jQM styling applied.

According to the documentation, I should simply invoke the page() function on the parent element, like $(selector).page().

However, other parts of the documentation suggest triggering the create event instead, for example: $(selector).trigger("create").

The issue is that neither of these methods seems to work - the jQM styling is still not being applied.

After examining the jQM source code, I attempted triggering the pagecreate event on the element which actually worked. However, this method is not officially documented, leaving me unsure about its future compatibility with jQM releases.

Additionally, I came across a mention in the documentation stating that calling page() on a page should only be done once..

In any case, is there a clear or standard way to instruct jQM to "enhance" the entire element along with its child elements? Or would it be advisable to continue using the pagecreate event trigger?

Thank you!

Answer №1

If you want to recreate an entire page, you can simply use the following code:

$(selector).trigger("pagecreate");

I provided a similar solution in response to another question here: . You can see an example of page recreation in that answer. Hopefully, this will help solve your issue.

Answer №2

What is the purpose of $(selector).trigger("create");

You can use this method to dynamically add elements to a page before JQM styling is applied during the 'pageshow' event. For instance, you can add a header/footer like this:

$(document).on('pagecreate', "[data-role=page]", function() {

var header = "<div data-role='header'>some header stuff</div>";
 var footer= "<div data-role='footer'>some footer stuff</div>";

$(this).prepend(header);
$(this).append(footer);

$("[data-role=header]").fixedtoolbar({tapToggle: false});
$("[data-role=footer]").fixedtoolbar({tapToggle: false});

});

Ensure that you are using jQuery version 1.7 or above as the on method is required for this functionality.

If you are facing issues with DOM generation and page editing, try loading the page first and then make dynamic changes to the DOM structure.

EDIT Set the reload page option to true

  $.mobile.changePage($(page), {reloadPage: true});

Edit 2

$(selector).children().each(function(){
   $(this).trigger('create');
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Database hosted on Heroku platform

Curious to know, if you utilize Heroku for hosting, what database do you prefer? Do you lean towards PostgreSql, MongoDB, or another option altogether? I initially developed a bot using sqlite3, but quickly discovered that Heroku does not support it and ...

What strategies can be used to effectively structure CSS and JavaScript in a project for optimal organization?

In my NetBeans project, I currently have a large web project with CSS files included in the header. Some CSS codes are needed on all pages, while others are only necessary for specific pages. I am looking to optimize high-traffic pages by removing any ...

Node.js tutorial: Matching aggregations by UserId

I have been developing a sample application in React where I have utilized aggregations. One of the functionalities involves implementing a query based on user ID that matches the status and retrieves the MAXDate and MINDate fields. How can I retrieve the ...

AngularJS Form Validation Error Handling

I am in the process of implementing Form Validation using AngularJS, and I have come across a scenario involving ng-class that is confusing me. Could someone please explain why they are utilizing ng-class in this manner? The presence of a map and an arra ...

What is the reason for all the buttons activating the same component instead of triggering separate components for each button?

I am facing an issue with my parent component that has 3 buttons and 3 children components. Each button is supposed to open a specific child component, but currently all the buttons are opening the same child component when clicked. The children components ...

Is it possible to dynamically retrieve an element's style from @ViewChild in an Angular 2 component without needing an active approach?

For instance, there's an element in the template that uses a local variable #targetElement to retrieve its current width whenever needed. However, I prefer not to calculate the style programmatically. I attempted using a setter with the @ViewChild ann ...

"Embracing Progressive Enhancement through Node/Express routing and the innovative HIJAX Pattern. Exciting

There may be mixed reactions to this question, but I am curious about the compatibility of using progressive enhancement, specifically the HIJAX pattern (AJAX applied with P.E.), alongside Node routing middleware like Express. Is it feasible to incorporate ...

What is the best way to save high-resolution images created with HTML5 canvas?

Currently, there is a JavaScript script being used to load and manipulate images using the fabricjs library. The canvas dimensions are set to 600x350 pixels. When smaller images are uploaded onto the canvas and saved as a file on disk, everything works c ...

Ways to combine extensive value within an array containing various objects

I am working with an array of objects and I need to merge all the values of params within each object into a single object. Array [ Object { "key": "This week", "params": Object { "thisWeekfilterDistance": [Function anonymous], "this ...

Two liquid level divs within a container with a set height

I hesitated to ask this question at first because it seemed trivial, but after spending over 3 hours searching on stackoverflow and Google, I decided to give it a shot. The issue I'm facing can be found here: http://jsfiddle.net/RVPkm/7/ In the init ...

What is the best way to add Vue dependency using CDN?

For my project which is built using Kendo, Vue, .Net, Angular and jQuery, I need to incorporate https://www.npmjs.com/package/vue2-daterange-picker. <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-c ...

Issue encountered when trying to import @tensorflow/tfjs-node in conjunction with the face-api.js library within a node.js environment

Using the @tensorflow/tfjs-node package along with face-api.js to enhance performance, here is my code snippet: // Import nodejs bindings to native tensorflow, // Not required but will significantly speed up operations (Python required) require('@tens ...

Is it possible to alter the value and label of an HTML button in a permanent manner?

I am developing a personalized management system that records your activities from the previous day based on the buttons you clicked. I have successfully implemented the ability to edit these activity buttons using jQuery, but I would like these changes to ...

Tips for migrating an AngularJS application to Angular

My current project involves implementing a basic search function using AngularJS (link). I want to integrate this feature into an existing Angular application. To do this, I created a new Angular app and transferred the view to app.component.html. <hea ...

Struggling to retrieve the value from ng-model?

Currently, I am utilizing this account due to being logged into Facebook on my other account and not having access to my phone for the verification code process. On another note, I am struggling to retrieve the value from an ng-model despite numerous atte ...

I lose my user interface after a series of clicking events

I've created a React application that generates new quotes without repetition, but I'm encountering an issue where the UI disappears after clicking enough times. What could be causing this behavior in my code? The console displays an object error ...

Exploring the functionality of promises in JavaScript

Currently, I am using the most recent version of Angular. The code snippet I've written looks like this: $q.all({ a: $q.then(func1, failHandler), b: $q.then(func2, failHandler), c: $q.then(func3, failHandler), }).then(func4); Is it guaranteed ...

Obtain the text content in cases where the DOM is missing both the title and value fields

I am trying to extract the value from a text-box using selenium, but the text-box is marked as readonly and aria-live is set to off. Here is the DOM structure: <input autocomplete="off" class="abc" type="text" role="t ...

Navigating efficiently with AngularJS directives

Hello, I am currently searching for a solution to dynamically modify the navigation links within the navigation bar based on whether a user is logged in or not. Of course, a logged-in user should have access to more pages. I have developed a UserService t ...

Tips for modifying the settings of a current google chart within a wrapper

Is there a way to update the options of an existing Google chart? For instance, if I want to apply these options to a chart with just a click of a button: var newOptions = { width: 400, height: 240, title: 'Preferred Pizza Toppings', col ...