0

I have a list of check boxes with different values.

<input type="checkbox" value="55" id="myId">
<input type="checkbox" value="65" id="myId">
<input type="checkbox" value="75" id="myId">
<input type="checkbox" value="85" id="myId">
<input type="checkbox" value="95" id="myId">

When I'm getting those values using js that will take only value=55 only. It is due to the same id="myId"

var x = "";
$("input[type='checkbox']").change(fucntion(){ 
   if(this.checked){
     x = x+","+x;
   }
});

When run that will load only 55 values like-: 55,55,55,55

Mamun
  • 66,969
  • 9
  • 47
  • 59
Steve Smith
  • 11
  • 1
  • 6
  • Possible duplicate of [jQuery checkbox values to comma separated list](https://stackoverflow.com/questions/9593237/jquery-checkbox-values-to-comma-separated-list) – Heretic Monkey Jul 21 '18 at 16:23

3 Answers3

3

Attribute id should be unique. You can use an array instead of string variable. Then simply add or remove item based on the check box status:

var x = [];
$("input[type='checkbox']").change(function(){
  if(this.checked){
    x.push(this.value);
  }
  else {
    var index = x.indexOf(this.value);
    x.splice(index, 1);
  }
  console.log(x.join(','));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value="55" id="">55
<input type="checkbox" value="65" id="">65
<input type="checkbox" value="75" id="">75
<input type="checkbox" value="85" id="">85
<input type="checkbox" value="95" id="">95
Mamun
  • 66,969
  • 9
  • 47
  • 59
1

If selection order isn't important can map() the checked values to new array every change

Your string concatenation approach doesn't take into account unchecking a previously checked input

$(':checkbox').change(function(){
   var vals = $(':checkbox:checked').map(function(){
       return this.value
   }).get()
   console.log(vals.join())
})
// insert values as text for demo
.wrap(function(){
   return $('<label>',{text:this.value})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value="55" >
<input type="checkbox" value="65" >
<input type="checkbox" value="75">
<input type="checkbox" value="85" >
<input type="checkbox" value="95" >

Note that ID's must be unique in a page

charlietfl
  • 170,828
  • 13
  • 121
  • 150
1

First of all don't use multiple same ids on your page, id should be unique on entire page, try data attributes instead

$("input[type='checkbox']").change(function(){
    var x = "";
    $("[data-id=myId]").each(function(){
      if(this.checked){
     x = x + $(this).val() + ",";
      }
    });
    console.log(x);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value="55" data-id="myId">
<input type="checkbox" value="65" data-id="myId">
<input type="checkbox" value="75" data-id="myId">
<input type="checkbox" value="85" data-id="myId">
<input type="checkbox" value="95" data-id="myId">
zdolny
  • 999
  • 1
  • 11
  • 21