Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping CSS when dynamically creating checkboxes with JQuery/JQM

I have some code that takes values from a database, and uses them to create a set of checkboxes dynamically:

$("#cTable").append('<fieldset data-role="controlgroup"> <legend>Option</legend>');

cDest = "includes/loadCategories.php"; 

$.getJSON(cDest, function(data) {
    cInfo = data.items;
    $.each(cInfo, function(index, info) {

        $("#cTable").append('<input type="checkbox" name="cat" id="cat_' + info.S_ID + ' class="ui-checkbox" value="' + info.S_ID + '" /><label for="chkCat_' + info.S_ID + '">' + info.S_Name + '</label> </br>');
    });
    $("#cTable").append('</fieldset>');
}); 

The issue is that these newly created checkboxes don't take on the JQuery Mobile CSS styling. Is there any way to assign the JQM css to this dynamically created element?

I thought about using the JQuery .CSS() function, but as I don't actually know the CSS needed (as it is part of default JQM), I dont think i can use this method.

Thanks

like image 566
Matthew Albone Avatar asked Apr 23 '26 01:04

Matthew Albone


1 Answers

You need to refresh the jQM Controls:

  • http://jquerymobile.com/demos/1.1.0-rc.1/docs/forms/checkboxes/methods.html

JS

$("input[type='checkbox']").checkboxradio("refresh");
like image 93
Phill Pafford Avatar answered Apr 26 '26 10:04

Phill Pafford



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!