Salvatore Ucchino
Salvatore Ucchino

Reputation: 727

jQuery Mobile - adding list of checkbox dynamically

i'm trying to add a list of checkbox dynamically but it does not show properly with mobile style.

here the js code:

var name = "option";
var id = "id";

$("#frame").html('<fieldset data-role="controlgroup"><legend>Seleziona le categorie da eliminare:</legend></fieldset>');

for (var i = 0; i < 4; i++) {
  $("fieldset").append('<input type="checkbox" name="' + name + '" id="' + id + '"><label for="' + name + '">' + name + '</label>');
}

$("#frame").append('<a href="#" data-role="button" data-inline="true" id="btndelcat">Elimina</a>');

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

Code on editor JSFiddle.

Upvotes: 2

Views: 8076

Answers (1)

Omar
Omar

Reputation: 31732

You have a mistake in creating checkboxes. Label's for attribute should match checkbox's id.

From this:

$("fieldset").append('<input type="checkbox" name="' + name + '" id="' + id + '"><label for="' + name + '">' + name + '</label>');

To this:

$("fieldset").append('<input type="checkbox" name="' + name + '" id="id' + i + '"><label for="id' + i + '">' + name + '</label>');

Demo

Upvotes: 3

Related Questions