2

I made few checkboxes along with a button which on submit calls a function where I want to get all the values of checkboxes which have been checked.

Code for creating those checkboxes is:

for (var loop=0; loop < count; loop++)
{

  var chap  =   document.createElement("input");
  chap.type =   "checkbox";
  chap.value    =   nearby[loop];
  chap.id   =   nearby[loop];

  document.getElementById("mapdisplay").appendChild(chap);
  var nearo = nearby[loop];
  var s     =   document.getElementById("mapdisplay");
  var text  =   document.createTextNode(nearby[loop]);
  s.appendChild(text);
  var br        =   document.createElement('br');
  s.appendChild(br);
}

Now I want to retrieve the values which are checked. I am trying this (but to no available)

function fsearch()
{
    var p       =   x;
    var narr    =   new Array();
    narr=nearby;//a global arr
    var checked_vals = new Array(); 
    $('#mapdisplay input:checkbox:checked').foreach()

             {
              checked_vals.push(this.value);

             }

Please suggest something to retrieve the checked values id of generated values are in array form. I can not use $("#nearby[0]").val().

2 Answers 2

4
var checkedCheckBoxesValueArray = $('#mapdisplay input:checkbox:checked').map(
  function(){
    return this.value;
}).get();
Sign up to request clarification or add additional context in comments.

Comments

1

Correct syntax would be:

$('#mapdisplay input:checkbox:checked').each(function(index) {
    checked_vals.push($(this).val());
});

Live test case: http://jsfiddle.net/e6Sr3/

Comments

Your Answer

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