ehsan ahmadi
ehsan ahmadi

Reputation: 498

How can i select all input that are selected in jquery?

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>

Upvotes: 0

Views: 45

Answers (4)

Pranav C Balan
Pranav C Balan

Reputation: 115212

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.

Upvotes: 2

Dhara Parmar
Dhara Parmar

Reputation: 8101

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

Upvotes: 1

mohsen kholaseshoar
mohsen kholaseshoar

Reputation: 347

Use Can Use This Code

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

Upvotes: 2

GreyRoofPigeon
GreyRoofPigeon

Reputation: 18113

You can use:

$('input:checked') 

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

Upvotes: 1

Related Questions