tonyf
tonyf

Reputation: 35587

Checkbox parameter passing to check for checked values only

I am unsure how to setup the following to check for checked items, i.e.:

function selectorToArray(pSelector){
  var lArray = [];
  $("input[name='f19']:checked").each(function(){
      lArray.push($(this).val());
  });
  return lArray;  
};

var lf19 = [];
lf19 = selectorToArray("input[name=f19]");

Basically within the function selectorToArray(pSelector), I do not want to use:

$("input[name='f19']:checked").each(function(){

as this is hardcoded; instead I want to use $(pSelector:checked).each(function(){ but this doesn't work.

I am looking for the correct syntax for only looking for checked values based on pSelector passed in as part of my .each function.

Upvotes: 1

Views: 238

Answers (2)

Deepak Ingole
Deepak Ingole

Reputation: 15772

Simply use,

$(pSelector + ":checked").map(function () {
     return $(this).val();
}).get();

Upvotes: 0

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67217

Try to concatenate the passed the value,

$(pSelector + ":checked")

The above code will be evaluated as,

$("input[name=f19]:checked")

Upvotes: 1

Related Questions