00 Votes

jQuery Mobile: Apply Style for dynamically created Content

Question by Guest | 2015-05-18 at 12:07

I am using jQuery Mobile for a Web-App. On some pages, I am dynamically loading some content depending on the user input and selection.

However, there is a problem with this dynamically loaded content: the jQuery Mobile Styles for the graphical user interface elements are not applied. For example, this affects input boxes, input select boxes but also the normal Listview or the Collabsible Set.

Apparently, the styling is applied when first loading the page and it has to be repeated after new contents has been loaded. Is there any possibility to manually execute this styling again?

ReplyPositiveNegative
0Best Answer0 Votes

You cannot execute the functions .trigger("create") or .enhanceWithin() on the new elements in order to apply the style.

For example, if you have loaded dynamic content into the DIV box with the ID "divid", you can use the following procedure:

$("#divid").enhanceWithin();

With this, all elements within the DIV "divid" will be styled again. The function .trigger("create") will do the same, but this function is marked as depreciated since jQuery Mobile version 1.4, so it is better to use .enhanceWithin() instead.

If you would like to have all in one line, you can do this:

var newHTML = '<p>Neuer Inhalt</p>';
$("#divid").html(newHTML).enhanceWithin();

Our new content is stored in the variable newHTML. We are adding this content to the DIV box "divid" and afterwards we are refreshing the style.

It is also possible to address single elements, for example:

$("select").selectmenu("refresh");
$("ul").listview("refresh");
$(".myclass").collapsibleset("refresh");

With the first line, for example, we are updating the menu of a select box which can be done after adding new elements. The example above updates all listviews and with using the third example, we unable to refresh or collapsible sets of the class "myclass".
2015-05-18 at 12:27

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.