This question already has answers here:
How to retrieve checkboxes values in jQuery
(14 answers)
Closed 9 years ago.
I am looking to get all checkboxes' VALUE which have been selected through jQuery.
You want the :checkbox:checked selector and map to create an array of the values:
var checkedValues = $('input:checkbox:checked').map(function() {
return this.value;
}).get();
If your checkboxes have a shared class it would be faster to use that instead, eg. $('.mycheckboxes:checked'), or for a common name $('input[name="Foo"]:checked')
- Update -
If you don't need IE support then you can now make the map() call more succinct by using an arrow function:
var checkedValues = $('input:checkbox:checked').map((i, el) => el.value).get();
Related
This question already has answers here:
How to retrieve checkboxes values in jQuery
(14 answers)
Closed 9 years ago.
I am looking to get all checkboxes' VALUE which have been selected through jQuery.
You want the :checkbox:checked selector and map to create an array of the values:
var checkedValues = $('input:checkbox:checked').map(function() {
return this.value;
}).get();
If your checkboxes have a shared class it would be faster to use that instead, eg. $('.mycheckboxes:checked'), or for a common name $('input[name="Foo"]:checked')
- Update -
If you don't need IE support then you can now make the map() call more succinct by using an arrow function:
var checkedValues = $('input:checkbox:checked').map((i, el) => el.value).get();
This question already has answers here:
How to retrieve checkboxes values in jQuery
(14 answers)
Closed 9 years ago.
I am looking to get all checkboxes' VALUE which have been selected through jQuery.
You want the :checkbox:checked selector and map to create an array of the values:
var checkedValues = $('input:checkbox:checked').map(function() {
return this.value;
}).get();
If your checkboxes have a shared class it would be faster to use that instead, eg. $('.mycheckboxes:checked'), or for a common name $('input[name="Foo"]:checked')
- Update -
If you don't need IE support then you can now make the map() call more succinct by using an arrow function:
var checkedValues = $('input:checkbox:checked').map((i, el) => el.value).get();
This question already has answers here:
How to get the attributes of a HTML element using JQuery?
(4 answers)
Closed 8 years ago.
I am looping through drop down option and checking attribute.
if attribute match than counter is increase. At the end i show counter as alert.
This is my code but some how its not working dont know why
var count= 0;
$('.mydropdown option').each(function () {
var level = this.attr("myattr");
if (level == "0") {
count++;
}
});
alert(count);
}
this is a plain javascript object, it does not contain a function called .attr()
Try,
var level = $(this).attr("myattr");
Just convert the this reference into a jquery object and invoke .attr() over it
This question already has answers here:
jQuery get values of checked checkboxes into array
(9 answers)
Closed 8 years ago.
Using javascript, how can I get the array of values selected in checkboxes used within a smarty html_options. Ive tried $('#selectbox').val() which returns null.
$("#trigger").click(function () {
var selected = $("input:checked");
var values = [];
selected.each(function (i,d) {
values.push(d.value);
});
console.log(values);
});
http://jsfiddle.net/698Ec/ this is a demo
This question already has answers here:
Get array of text values using selector instead of iterating?
(7 answers)
Closed 9 years ago.
I have html with multiple things classed .text. I the perform
console.log($(whatever).find('.foo').text());
The result is a textstring with all the contents in it. Can I save it to an array instead somehow?
You can use the map method to get this done
var textArray = $(whatever).find('.foo').map(function() {
return $(this).text();
}).get();
This will get you an array.. If you want to display it as a string then just use join along with it.
Check Fiddle
var arr = [];
$(whatever).find('.foo').each(function() {
arr.push($(this).text());
});