It is necessary to check every checkbox that is in the loop
<script>
$(document).ready(function(){
function countChecked() {
var n = $("input:checked").length;
$("div").text(n + (n <= 1 ? " is" : " are") + " checked!");
}
countChecked();
$(":checkbox").click(countChecked);
});
</script>
<style>
div { color:red; }
</style>
</head>
<body>
<form>
<input type="checkbox" name="newsletter" checked="checked" value="Hourly" />
<input type="checkbox" name="newsletter" value="Daily" />
<input type="checkbox" name="newsletter" value="Weekly" />
<input type="checkbox" name="newsletter" checked="checked" value="Monthly" />
<input type="checkbox" name="newsletter" value="Yearly" />
</form>
I need to get the value of all checked checkboxes like "Hourly", "Daily"
how can i get all the values โโof the checked checkboxes can anyone help me
0
a source to share
4 answers
slight improvement for Pim Jager (assuming you want an array of return values):
function getCheckedNames(){
var arr = new Array();
var $checked = $('[@name=newsletter]:checked');
$checked.each(function(){
arr.push($(this).val());
});
return arr;
}
and with improvement from duckyflip:
function getCheckedNames(){
return jQuery('[@name=newsletter]:checked').map(function(){
return this.value;
});
}
+1
a source to share
I think this is what you need:
function updateChecked(){
var arr = new Array();
$('input:checkbox:checked').each( function() {
arr.push($(this).val());
});
var checkboxesText = arr.join(', ');
var moreText = (arr.length <= 1 ? " is" : " are") + ' checked!';
$('div').text(checkboxesText + moreText);
}
This will cause the div text to look something like "Hourly, checked daily!"
0
a source to share