Note that there are some explanatory texts on larger screens.

plurals
  1. POProper formatting when adding dynamic content in jquery mobile
    primarykey
    data
    text
    <p>I am trying to dynamically add some content to a list of checkboxes in an collapsible <code>&lt;li&gt;</code> item in jQuery Mobile. I cannot get the proper formatting with the nice rounded corners and the grouped items. It gets even worse when I add other elements at the leaf level.</p> <p><a href="http://jsfiddle.net/xg8Ge/" rel="nofollow">Here</a> is an example that shows the problem (add jquery and jquerymobile scripts and CSS in the header obviously):</p> <pre><code>&lt;body&gt; &lt;div data-role="page" id="page"&gt; &lt;ul id="listList" data-role="listview"&gt; &lt;li id="list1" data-role="collapsible"&gt; &lt;h3&gt;list 1&lt;/h3&gt; &lt;div data-role="fieldcontain"&gt; &lt;fieldset data-role="controlgroup" id="fs1"&gt; &lt;input type="checkbox" id="cb1" /&gt;&lt;label for="cb1"&gt;text&lt;/label&gt;&lt;/fieldset&gt;&lt;/div&gt;&lt;/li&gt; &lt;li id="list2" data-role="collapsible"&gt; &lt;h3&gt;list 2&lt;/h3&gt; &lt;p&gt;here comes another list of checkboxes...&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt; &lt;input type="button" onclick="addCheckbox();" value="add a checkbox to list1" /&gt;&lt;/div&gt; &lt;/body&gt; &lt;script&gt; function addCheckbox() { $("#list1 fieldset").append('&lt;input type="checkbox" id="cb2" /&gt;&lt;label for="cb2"&gt;More text&lt;/label&gt;'); } &lt;/script&gt; </code></pre> <p>I tried to add <code>.page()</code> after the call to <code>append()</code>but it did not work properly though a bit better.</p> <p>Beyond my example, the generic question is how to dynamically append content to the DOM tree after jQuery Mobile has played around with the DOM structure. I believe there exists a function that "jquerymobilizes" part of the tree but I cannot find it.</p> <p>Thanks a lot for your help!</p> <p><em>EDIT: In short, I am trying to dynamically add elements into one <code>&lt;li&gt;</code>element of the <code>listview</code> and not trying to add elements to the list itself. Refreshing the <code>listview</code> does not seemt o help here.</em></p> <p><em>EDIT 2: I am getting a bit closer as I found the <code>.trigger("create")</code> function that can be chained to <code>.append()</code> (cf <a href="http://jquerymobiledictionary.pl/faq.html" rel="nofollow">JQM FAQ</a>). With the following script it works slightly better though the <code>data-role="controlgroup"</code> does not provide the right formatting with nice rounded boxes.</p> <pre><code>$("#list1 fieldset").append('&lt;input type="checkbox" id="cb2" /&gt;&lt;label for="cb2"&gt;More text&lt;/label&gt;').trigger("create"); </code></pre> <p>I will post the full answer if I get there at some point.</p> <p>EDIT 3: Here is my example illustrated in <a href="http://jsfiddle.net/xg8Ge/" rel="nofollow">jsFiddle</a></em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload