-1

what is the easiest way to get the value of a checkbox? I create my checkboxes dynamically

 function buildList() {
    var output;
    output = "<form><fieldset data-role='controlgroup' data-iconpos='right' id='fieldset_item'>";
    if (codeCounter != 0 && codeCounter > 0 && codeCounter != null) {
        for (var i = 0; i < codeCounter; i++) {
            output += "<input type='checkbox' name='checkbox_" + i
                    + "' id='checkbox_" + i + "'><label for='checkbox_" + i
                    + "'>" + localStorage.getItem(i) + "</label>";
        }
    }

    output += "</fieldset></form>";
    $(output).appendTo("#fieldSet");
    $('#fieldSet').trigger("create");
}

So how can i get the value of the each checkbox?

Erdem Güngör
  • 867
  • 5
  • 14
  • 27

2 Answers2

1

Having no value in the checkboxes you're generating, kind of made me feel you want the text inside the label tags, in case I was right:

var id = 'checkbox_1'; 
var checkboxLabel = $('label[for="' + id + '"]').html();
Taher
  • 11,902
  • 2
  • 28
  • 44
0
$('input[type=checkbox]:checked').val()
Ken Wheeler
  • 1,938
  • 12
  • 12
  • Which will return the value of the first checked checkbox and ignore the rest (or undefined if none are checked). – RobG Apr 17 '14 at 12:43
  • It was more of an example showing how to used the checked pseudo selector. Hence, the easiest way to get the value of A checkbox. – Ken Wheeler Apr 17 '14 at 12:53