0

How can i get all input that are checked in jQuery?

<div>
<input type="checkbox" checked="checked">sony </input>
<input type="checkbox">samsung </input>
<input type="checkbox"> Other</input>
</div>
Alan Moore
  • 6,525
  • 6
  • 55
  • 68
ehsan ahmadi
  • 498
  • 2
  • 5
  • 7

4 Answers4

2

You can use :checked pseudo-selector

$(':checked')

console.log(
  $(':checked').length
)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <input type="checkbox" checked="checked">sony
  <input type="checkbox">samsung
  <input type="checkbox" checked>Other
</div>

FYI : There are various bugs in your code :

  1. There is attribute like cheched, the answer is based on checked attribute.
  2. Input tag is self closing so </input> is invalid.
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
2

Use Can Use This Code

$('input[type="checkbox"]:checked')
1

You can use:

$('input:checked') 

More info: https://api.jquery.com/checked-selector/

GreyRoofPigeon
  • 17,833
  • 4
  • 36
  • 59
1

You can try this:

<div>
<input type="checkbox" name="checkboxlist" checked="checked"> sony </input>
<input type="checkbox" name="checkboxlist"> samsung </input>
<input type="checkbox" name="checkboxlist" checked="checked"> Other</input>
</div>

jquery:

 var checkValues = $('input[name=checkboxlist]:checked').map(function()
 {
     return $(this).val();
 }).get(); // it will find list of all checked selectboxes
Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27