Reputation: 708
Hi I have checboxes with different names:
Names - hours, and value - day. For example: value="1" means Monday, and name="0609" means from 06am - 09am.
<input type="checkbox" value="1" name="0609">
<input type="checkbox" value="2" name="0609">
...
<input type="checkbox" value="7" name="0609">
<input type="checkbox" value="1" name="0912">
<input type="checkbox" value="2" name="0912">
...
<input type="checkbox" value="7" name="0912">
I need to take all checked and non checked inputs and combine into array by the name like for example: If all days is checked except monday
'0609' => 0, 1, 1, 1, 1, 1, 1
How can I solve this ?
I was trying to get with .map but I get ungrouped values...
var valuess = $('input:checked').map(function() {
return this.value;
}).get();
console.log(valuess);
UPDATE:
var obj = {}; // Define object
// Loop over all checkboxes
$('input[type=checkbox]').each(function() {
var name = $(this).attr('name'); // Get name of this checkbox
var sThisVal = (this.checked ? "1" : "0");
if (obj[name]) {
obj[name].push(sThisVal); // Push value
} else {
obj[name] = [sThisVal]; // Create array and push value
}
});
console.log(obj);
Upvotes: 0
Views: 1702
Reputation: 2144
Here is the fiddle http://jsfiddle.net/pratbhoir/d8p04tn5/
function getData() {
var obj = {}; // Define object
// Loop over all checkboxes
$(':checkbox').each(function () {
var name = $(this).attr('name'); // Get name of this checkbox
var isChecked = 0;
if ($(this).is(":checked")) {
isChecked = 1;
}
var dayIndex = $(this).val() - 1; //Get the dayIndex from value
if (obj[name]) {
obj[name][dayIndex] = isChecked; //set 1 to the value
} else {
obj[name] = [0, 0, 0, 0, 0, 0, 0]; // Create array and push value
obj[name][dayIndex] = isChecked; //Set 1 to the array
}
});
console.log(obj);
}
Upvotes: 0
Reputation: 990
var obj = {};
obj.checked = {};
obj.unchecked = {};
$(':checkbox').each(function() {
//console.log(this);
$this = $(this);
var key = $this.attr("checked") ? "checked" : "unchecked";
var index = $this.attr("name");
if(!obj[key][index]) {
obj[key][index] = [];
}
obj[key][index].push($this.val());
});
console.log(obj);
Upvotes: 0
Reputation: 11750
First, loop through all checkboxes, get their name attributes and make an array of then (without duplicates).
Then, loop through this array and create the object that logs out at the end.
Check the console and look at the object you are getting.
var nm = [];
$('input[type="checkbox"]').each(function() {
var name = $(this).attr('name');
if ($.inArray(name, nm) == -1) {
nm.push(name);
}
});
var arr = [];
for (i=0;i<nm.length;i++) {
var obj = {};
obj.name = nm[i];
var days = obj.days = {};
$('input[name=' + nm[i] + ']').each(function() {
var that = $(this);
var val = that.attr('value');
if (that.is(':checked')) {
days[val] = 1;
} else {
days[val] = 0;
}
});
arr.push(obj);
}
console.log(arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" value="1" name="0609">
<input type="checkbox" value="2" name="0609" checked>
<input type="checkbox" value="7" name="0609">
<input type="checkbox" value="1" name="0912">
<input type="checkbox" value="2" name="0912">
<input type="checkbox" value="7" name="0912">
Upvotes: 0
Reputation: 4844
I think you can do this
$("input[name='0609']").each( function () {
//do somethin
});
Upvotes: 0
Reputation: 779
Check this:
var obj = {}; // Define object
// Loop over all checkboxes
$(':checkbox').each(function() {
var name = $(this).attr('name'); // Get name of this checkbox
if (obj[name]) {
obj[name].push($(this).val()); // Push value
} else {
obj[name] = [$(this).val()]; // Create array and push value
}
});
console.log(obj);
Upvotes: 2