0

I want to select all value checked. My checkbox name are type[]

When I remove the [] it is working . Please see my code below

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"> </script> 
  

      
    <input type="checkbox" name="type[]" value="GFG" id="feaut" /> GFG: 
    <input type="checkbox" name="type[]" value="Geeks"  id="feaut" /> Geeks: 
    <input type="checkbox" name="type[]" value="Geek"  id="feaut"/> Geek: 
    <input type="checkbox" name="type[]" value="portal"  id="feaut" /> portal: 
    <br> 
    <button> 
        click here 
    </button> 

    <script> 
        $('button').on('click', function() { 
            var array = []; 
            $("input:checkbox[name=type]:checked").each(function() { 
                array.push($(this).val()); 
            }); 
            console.log(array.toString()); 
        }); 
    </script> 
Agent69
  • 648
  • 1
  • 8
  • 16
  • Hi just use `*` i.e :`$("input:checkbox[name*=type]:checked")` so it will match anything after `type` – Swati Oct 30 '20 at 04:45
  • Does this answer your question? [jQuery selector for inputs with square brackets in the name attribute](https://stackoverflow.com/questions/2364982/jquery-selector-for-inputs-with-square-brackets-in-the-name-attribute) – freedomn-m Oct 30 '20 at 10:58

1 Answers1

1

Use [type=checkbox] to select inputs of type checkbox. Also, since [] are illegal characters in unquoted selector values, surround them in quotes: [name='type[]']

$('button').on('click', function() {
  var array = [];
  $("input[type=checkbox][name='type[]']:checked").each(function() {
    array.push($(this).val());
  });
  console.log(array.toString());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
</script>



<input type="checkbox" name="type[]" value="GFG" id="feaut" /> GFG:
<input type="checkbox" name="type[]" value="Geeks" id="feaut" /> Geeks:
<input type="checkbox" name="type[]" value="Geek" id="feaut" /> Geek:
<input type="checkbox" name="type[]" value="portal" id="feaut" /> portal:
<br>
<button> 
        click here 
    </button>
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320