0

Dynamic checkboxes are created using the below code.

    var list = new Array();
    for(var i=0; i<data.length; i++)
    {
         $('#cContainer').append('<input type="checkbox" name="catChkBox" class="ckbox"
          id = "'+ data[i].id +'" value="'+ data[i].id + '" /> ' + data[i].name + '<br 
          />');
    }

The above code is inside document.ready function. I want to collect the checked values ie. data[i].name outside document.ready and put it in a list.

I tried the below code which displays only the id. But what I need is the text. Here it is data[i].name (I get this from dwr call)

function showSelectedValues()
{
  alert("--------------" + $("input[name=catChkBox]:checked").map(
     function () {return this.value;}).get().join(","));
}

I will call showSelectedValues() from some other method where I will make use of the values. Any idea for this???

1
  • hey , why you aren't marking none of answers to your questions as accepted ! Commented Apr 16, 2013 at 15:37

3 Answers 3

2
$("input[name=catChkBox]:checked").val();
Sign up to request clarification or add additional context in comments.

4 Comments

@llya it prints only the id and not the name (data[i].name)
because the "value" and "id" of the checkbox contain data[i].id in your example data[i].name is not a part of the checkbox
because the "value" and "id" of the checkbox contain data[i].id in your example data[i].name is not a part of the checkbox
@llya but i want only the options. Is there any way to do it ?
1

for each checkbox add a label

var list = new Array();
for(var i=0; i<data.length; i++)
{
     $('#cContainer').append('<input type="checkbox" name="catChkBox" class="ckbox"
      id = "'+ data[i].id +'" value="'+ data[i].id + '" /><label for="'+ data[i].id +'">' + data[i].name + '</label><br/>');
}

then use code below to get text associated with each checked checkbox

$("input[name=catChkBox]:checked + label").text();

update :

to save values into the array use the code below :

function ConvertSelectedValuesToList()
{
  list = $("input[name=catChkBox]:checked + label").map(function () {
      return $(this).text();
  }).get();
}

if you want to create a comma separated string of values then add join(",") after get() .but then it's not an array anymore.

2 Comments

Wonderful. I want to store the options into a list as a comma separated one. Any clues for it ?
@sahana : if you want a comma separated one then it's not a list , it's a string .
1

data[i].name is never written to the input element and as such is not part of it. It is merely text following the element.

I would recommend to add a data-attribute also containing the value you are interested in, similar to this:

var list = new Array();

for (var i = 0; i < data.length; i++) {
    $('#cContainer').append('<input type="checkbox" data-name="' + data[i].name + '" name="catChkBox" class="ckbox" id = "' + data[i].id + '" value="' + data[i].id + '" /> ' + data[i].name + '<br/>');
}

Note data-name="' + data[i].name + '" above.

Then you can update your method to simply query the data attribute, similar to this:

function showSelectedValues() {
    alert("--------------" + $("input[name=catChkBox]:checked").map(function () {
        return $(this).data('name');
    }).get().join(","));
}

DEMO - Using the data-attribute


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.