Question

This is my code: http://jsfiddle.net/YKvR3/34/

I would create a controlgroup with values that are in my array (name). The problem is that when I click load button the values are added in a controlgroup but the jquery-ui styles are not loaded like in the image. The controlgroup is not styled with jquery-ui mobile css.

$("#load").click(function(){
var name=["one","two"];
var html='<fieldset id="listPlayers" data-role="controlgroup"><legend><h1>Choose as many players as youd like to remove:</h1></legend>';
for ( var int = 0; int < 2; int++) {
        html+='<input type="checkbox" name="checkbox-'+int+'a" id="checkbox-'+int+'a" class="custom" /><label for="checkbox-'+int+'a">'+name[int]+'</label>';
    }
    alert('<legend><h3>Choose as many players as you would like to remove:</h3></legend>'+html+'</fieldset');
    $("#list").html(html+'</fieldset');       
//$("#list").page();});​

enter image description here What I am doing wrong? Thanks.

Was it helpful?

Solution

$("#list").trigger('create');

From: jqm docs

if you generate new markup client-side or load in content via Ajax and inject it into a page, you can trigger the create event to handle the auto-initialization for all the plugins contained within the new markup. This can be triggered on any element (even the page div itself), saving you the task of manually initializing each plugin (listview button, select, etc.).

OTHER TIPS

I do applogies if this post is too old and if my post isn't by the correct standard since it's the first time ever posting so please correct me if it's horribly bad :-]

But in case someone else comes across it, I had similar problems with how the dynamic data is displayed and I used the jsfiddles and comments above as a help, and this is what got mine to work, well somewhat near my solution, I don't have a button to load the data it's loaded automatically when the page is loaded.

Updated In my .html-file:

<div id="members"></div>     
<input type="button" id="load" value="test"/>

Updated In my .js-file:

$("#load").click(function(){

var name = ["NameOne","NameTwo", "NameThree"];
var fset = '<fieldset data-role="controlgroup" id="members-ctrlgroup"><legend>This is a legend:</legend>';
var labels='';

    for ( var i = 0; i < name.length; i++) {

        labels  += '<input type="checkbox" class="checkbox" id="c'
                + i
                + '"><label for="c'
                + i
                + '" data-iconpos="right">'
                + name[i]
                +'</label>';
    }
    $("#members").html(fset+labels+'</fieldset>');       
    $("#members").trigger("create");
});

I know the "field" looks a bit weird how I divided it but I find it somewhat easier when it comes to getting the whole html-string correct in these cases.

Updated In order to have the rounded corners and have it as one controlgroup you'll have to have this approach instead. Just like the former posters showed. Do note that the id with the checkbox and the label for= can tend to screw the output if they're not the same :-]

fiddle

In order to replace the content you should use .html(); instead of .append(), which adds the new content after the existing one. After adding content to a jQuery Mobile Page you need to enhance the content, using for instance $("input[type='radio']").checkboxradio();

I was using

for( var i=0 ; i < html.length ; i++ ){

                        var spline = html[i].split("|");

                        inHTML = inHTML +  " <input type=\"checkbox\" name=\"checkbox-"+i+"a\" id=\"checkbox-"+i+"a\" class=\"custom\" /> <label for=\"checkbox-"+i+"a\">"+ spline[0] +" , "+ spline[2] +"</label> ";


                    }

                    jq("fieldset#myFieldSet").empty();
                    jq("fieldset#myFieldSet" )
                    // Append the new rows to the body
                    .append( inHTML )
                    // Call the refresh method
                    .closest( "fieldset#myFieldSet" )

                    // Trigger if the new injected markup contain links or buttons that need to be enhanced
                    .trigger( "create" );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top