5

Possible Duplicate:
Dynamically adding collapsible elements

I am dynamically creating a Collapsible set and adding it to the page using $('#myID').html(htmlcode). The styling is not being applied to the page. How can I get jQUery mobile to apply it styling?

(I am using $.get() to query a web service.The content that returned I am looping through to create the markup)

Community
  • 1
  • 1
Dappy
  • 251
  • 1
  • 2
  • 8

4 Answers4

5

This is working for me. My ajax returns a bunch of h3's which I insert into a collapsible div and append a p tag for the content.

$(document).ready(function(){

    $.get(my_url, function(data) {
        var content = $('div[data-role="content"]').html(data);
        $('h3').each(function(h3_element) {
            var coll = $('<div class="ui-collapsible-contain" name="blog"  data-role="collapsible" data-collapsed="true">');
            coll.append($(this));
            coll.append($('<p>'));
            content.append(coll);
        });
        content.trigger( "create" );        
    });

});
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
Bjorn
  • 455
  • 4
  • 13
3

Already answered in Jquery Mobile Collapsible Content

To initialize your div correctly after preventing the default init formating, you'll have to use something like $("a way to select your div").trigger('create'); as specified here: http://jquerymobile.com/demos/1.0b2/#/demos/1.0b2/docs/pages/page-scripting.html

Community
  • 1
  • 1
Jesus
  • 31
  • 2
1
$content.find(":jqmData(role=collapsible)").collapsible();
Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
Totoro
  • 156
  • 2
  • 13
  • 1
    This was how my team was doing it at first, but it caused problems with the styling when performed on a `collapsible-set` as well as the `collapsible`s inside. Using `.trigger('create')` fixed it. – lime Apr 08 '12 at 17:47
0

Collapsible content and Ajax loading

neoneye
  • 50,398
  • 25
  • 166
  • 151