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
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 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:
Swap value of two properties on object(s)
(3 answers)
Closed 8 years ago.
I have the following JSON. I need to swap SortId
Like I have this,
[{"CategoryId":1,"Name":"Worktable","SortId":1}
,{"CategoryId":2,"Name":"Bf ","SortId":2}]
After swaping their 'SortId' I need
[{"CategoryId":1,"Name":"Worktable","SortId":2}
,{"CategoryId":2,"Name":"Bf ","SortId":1}]
Please tell me how to do it through JavaScript.
var tmp = a[0].SortId;
a[0].SortId = a[1].SortId;
a[1].SortId = tmp;
jsFiddle
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());
});