1

I have a list of checkbox, my goal is to create and store the value of checkbox only the one that is checked. Can someone guide me with this?

<input type="checkbox" name="vehicle" value="Bike"/>
<input type="checkbox" name="vehicle" value="Car"/>
<input type="checkbox" name="vehicle" value="Airplane"/>
user2310422
  • 555
  • 3
  • 9
  • 22

5 Answers5

3

You really should show some code of what you have tried. You can get the checkboxes by name, then loop over them and collect the values of those that are checked:

<script>

function getCheckboxValues(form) {
  var values = [];
  var vehicles = form.vehicle;

  for (var i=0, iLen=vehicles.length; i<iLen; i++) {
    if (vehicles[i].checked) {
      values.push(vehicles[i].value);
    }
  }
  // Do something with values
  alert("Vehicles: " + values.join(', '));
  return values;
}

</script>

<form onsubmit="getCheckboxValues(this); return false;">
  Bike: <input type="checkbox" name="vehicle" value="Bike"><br>
  Car: <input type="checkbox" name="vehicle" value="Car"><br>
  Aeroplane: <input type="checkbox" name="vehicle" value="Airplane"><br>
  <input type="reset"> <input type="submit">
</form>
RobG
  • 142,382
  • 31
  • 172
  • 209
1

Please do one thing change name vehicle to vehicle[]

<input type="checkbox" name="vehicle[]" value="Bike"/>
<input type="checkbox" name="vehicle[]" value="Car"/>
<input type="checkbox" name="vehicle[]" value="Airplane"/>

at post you will receive the array of checked vehicle values and you can store it to DB

in $_POST['vehicle']

Colin Brock
  • 21,267
  • 9
  • 46
  • 61
Praveen kalal
  • 2,148
  • 4
  • 19
  • 33
0

Look on live demo

http://jsbin.com/uwewi

<script>
$('input[name=vahicle]').click(function() {
    $('input[name=vahicle]:checked').not(this).removeAttr('checked');
});
</script>

OR

jQuery - checkboxes like radiobuttons

If you want to get only one value among this than please you radio button its best way.

<input type="radio" name="vehicle" value="Bike"/>
<input type="radio" name="vehicle" value="Car"/>
<input type="radio" name="vehicle" value="Airplane"/>

OR

If you want to go with checkbox than you below jQuery;

<input type="checkbox" name="vehicle" value="Bike"/>
<input type="checkbox" name="vehicle" value="Car"/>
<input type="checkbox" name="vehicle" value="Airplane"/>
<script>
$('input[name=vahicle]').click(function(){
    $('input[name=vahicle]').removeAttr('checked');
    $(this).attr('checked', true);

});
</script>

Hope this will help you...!!

Community
  • 1
  • 1
Kishan Patel
  • 1,358
  • 10
  • 24
-2

If you use jQuery:

var vehicles = [];
$("input:checked").each(function() {
  vehicles.push($(this).val());
});

console.log(vehicles);
ijse
  • 2,998
  • 1
  • 19
  • 25
-2

Updated Answer

var input = document.getElementsByTagName('input')
,   value = {0:'', 1:'', 2:''}
,   x = input.length--
,   array
,   box
;
function handle (x) {

 input[x].onchange = function () {
   box      = input[x];
   value[x] = box.checked ? box.value : '';
   array    = [value[0], value[1], value[2]];

   console.log(array);       
 };
}

while (x) handle(--x);

Working fiddle

Jay Harris
  • 4,201
  • 17
  • 21