Echoing Throughout Tel Numara Echoing Throughout Tel Numara - 11 months ago 35
Javascript Question

Javascript : Pushing a value of checkbox if checked and the value of not checked together

So I have multiple checkboxes.

<input type="checkbox" value="special" name="special_round[]">special1
<input type="checkbox" value="special" name="special_round[]">special2
<input type="checkbox" value="special" name="special_round[]">special3
<input type="checkbox" value="special" name="special_round[]">special4

I can only do is to push value that has checked the input

$('#click').on('click', function(e){
var specialSequences = [];
$('input[name="special_round[]"]:checked').each(function(i, v) {


The output if i check the special1 and special4 is


How do i make it like this,

special, 0 , 0, special //or
special, " ", " ", special

//or if i checked the special2 and speial4 the output should
" ", special , " " , special

Answer Source

You need to iterate through all checkboxes and check which one is "checked" and which - is not.
Use the following approach:

  var specialSequences = [];
  $('input[name="special_round[]"]').each(function(i, v) {
    var current = ($(v).is(":checked"))? $(v).val() : "";

About jQuery .is() method: