Using jquery to get all checked checkboxes with a certain class name
$('.theClass:checkbox:checked')
will give you all the checked checkboxes with the class theClass
.
How to get all checked checkboxes
A simple for loop which tests the checked
property and appends the checked ones to a separate array. From there, you can process the array of checkboxesChecked
further if needed.
// Pass the checkbox name to the function
function getCheckedBoxes(chkboxName) {
var checkboxes = document.getElementsByName(chkboxName);
var checkboxesChecked = [];
// loop over them all
for (var i=0; i<checkboxes.length; i++) {
// And stick the checked ones onto an array...
if (checkboxes[i].checked) {
checkboxesChecked.push(checkboxes[i]);
}
}
// Return the array if it is non-empty, or null
return checkboxesChecked.length > 0 ? checkboxesChecked : null;
}
// Call as
var checkedBoxes = getCheckedBoxes("mycheckboxes");
Getting all selected checkboxes in an array
Formatted :
$("input:checkbox[name=type]:checked").each(function(){
yourArray.push($(this).val());
});
Hopefully, it will work.
Get a list of all checked checkboxes
About: %5B
%5D
Answer: They are simply raw HTTP encoded values of [
]
(result of serialize function).
When the server parses it, it converts it to []
and sends that to the application which will be treated as an array.
About why you are getting dummy: feature%5B%5D=on&feature%5B%5D=on...
string
Answer: You've forgot to give every checkbox a value parameter, then they will be like: feature%5B%5D=custom_css&feature%5B%5D=custom_js...
I've wrote solution.
Take this working example and handle "feature" param of request on server-side app like a string and shrink it by ,
(php: $features = explode(',', $_POST['features']
);
$(function() {
$('#getFeatures').click(function() {
var features = [];
$('.feature input[type="checkbox"]:checked').each(function() {
features.push($(this).val());
});
$('#selectedFeatures').html(features.join(','));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="feature">
<h2>Features</h2>
<label><input class="custom_css" checked="" type="checkbox" name="feature[]" value="custom_css"> Custom CSS (style.css)</label>
<label><input class="custom_js" checked="" type="checkbox" name="feature[]" value="custom_js"> Custom Javascript (script.js)</label>
<label><input class="modernizr" type="checkbox" name="feature[]" value="modernizr"> Modernizr</label>
<label><input class="google_maps" type="checkbox" name="feature[]" value="google_maps"> Google Maps</label>
<label><input class="custom_api" type="checkbox" name="feature[]" value="custom_api"> Custom API</label>
<label><input class="font_awesome" type="checkbox" name="feature[]" value="font_awesome"> Font Awesome</label>
</div>
<button id="getFeatures">GET FEATURES</button>
<div id="selectedFeatures"></div>
How to return all values of checked checkboxes?
You need to use $(this).val()
to get the value like this
function submit() { var values = []; $("input[type=checkbox]:checked").each(function() { values.push($(this).val()); }); alert(values.join());}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><input type="checkbox" name="top" class="checkbox" id="check1" value="black top" /><input type="checkbox" name="top" class="checkbox" id="check2" value="squish" value="blue something" /><button type="button" onclick="submit()">Click Me!</button>
How to get all the value of checked checkboxes using jquery/javascript?
You can simply use jQuery's .map()
and .get()
on all the checked check boxes:
$("label > span:contains('OTHERS')").prev().prop('checked', false);
var valor = $('input[type=checkbox]:checked').map(function(){ return this.value; }).get();console.log(valor);
$("label > span:contains('OTHERS')").prev().change(function(){ if(!this.checked) alert('Others unchecked');});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div class="form-group" id="documents"> <label> <input id="check_id3" type="checkbox" value="3" class="chk3" checked=""> <span>OTHERS</span> <br> </label> <div style="padding-bottom:5px"></div> <label> <input id="check_id1" type="checkbox" value="1" class="chk1" checked=""> <span>Invoice</span> <br> </label> <div style="padding-bottom:5px"></div> <label> <input id="check_id2" type="checkbox" value="2" class="chk2" checked=""> <span>Packing List</span> <br> </label> <div style="padding-bottom:5px"></div></div>
Get a list of checked checkboxes in a div using jQuery
Combination of two previous answers:
var selected = [];
$('#checkboxes input:checked').each(function() {
selected.push($(this).attr('name'));
});
Get all checked checkboxes javascript or jquery
Jquery
$('input[type=checkbox]:checked')
Using the :checked selector
https://api.jquery.com/checked-selector/
How to retrieve a list of checkboxes selected among all those of the form?
You can loop over them and iterate on the controls on the form.
foreach(Control c in this.Controls)
{
if(c is CheckBox)
{
// Do stuff here/logic
}
}
Or do a more LINQ/Lamba-ish type approach of
var checkList = YourForm.Controls.OfType<CheckBox>().Where(x => x.Checked).ToList();
checkList.ForEach(x => {
//Do stuff here
});
Related Topics
Angularjs - How to Get an Ngrepeat Filtered Result Reference
Difference Between "Process.Stdout.Write" and "Console.Log" in Node.Js
Watch for Object Properties Changes in JavaScript
Chrome Extension: Make It Run Every Page Load
Wrapping Lines/Polygons Across the Antimeridian in Leaflet.Js
Angularjs: How to Run Additional Code After Angularjs Has Rendered a Template
Using Lodash .Groupby. How to Add Your Own Keys for Grouped Output
What Are Closures and Callbacks
Socket.Io 1.X: Use Websockets Only
Orderby Multiple Fields in Angular
How to Sort/Order Keys in JavaScript Objects
Jquery - Create Hidden Form Element on the Fly
Jquery Ajax Requests Are Getting Cancelled Without Being Sent