I am trying to add values here:
var selected_value = {
addtypeid: addtypeid,
isnew: isnew,
geographicareaid: geographicareaid,
catid: catid,
catid2: catid2,
manufacturerid: manufacturerid,
modelid: modelid,
yearofmanufacturing_from: yearofmanufacturing_from,
yearofmanufacturing_to: yearofmanufacturing_to,
};
using this code:
var categories = [];
$("input[name='categoriesfilters[]']:checked").each(function () {
var id = $(this).attr("id");
var value = $(this).val();
categories[categories.length] = {
id: value
};
});
$.each(categories, function (id, value) {
//alert('id : ' + id + 'value:' + value);
selected_value.id = value;
});
however, only the last checked checkbox is added in the following format:
id[id] 166
while i would like to have there all the checkboxes in the format:
166:166,
Anyone can help me with this?
Regards, John
Xotic750
23.6k8 gold badges59 silver badges81 bronze badges
asked Feb 18, 2014 at 15:21
user2417624
6931 gold badge10 silver badges33 bronze badges
1 Answer 1
Try this:
$("input[name='categoriesfilters[]']:checked").each(function () {
var id = $(this).attr("id");
var value = $(this).val();
categories.push({
id: id,
value: value
});
});
$.each(categories, function (id, item) {
//alert('id : ' + id + 'value:' + value);
selected_value[item.id] = item.value;
});
answered Feb 18, 2014 at 15:32
ppoliani
4,9264 gold badges38 silver badges65 bronze badges
Sign up to request clarification or add additional context in comments.
6 Comments
user2417624
i get error in this line: categories.push(obj); // obj is not defined
ppoliani
are you sure you define the object above i.e. var obj = {}; By the way, are you sure the error says obj is not defined? This is a valid JavaScript code even if you do not define the obj. The only error i can imagine in that line is - categories is not defined
user2417624
you are right, sorry. however, now those values are not added in my selected_value...in the post in the console they are nowhere to be seen. any chance that we can solve this on chat?
ppoliani
if you provide a jsFiddle I'll try to sort it out for you
ppoliani
Can you check if the categories array has data?
|
lang-js
categories[categories.length] = { id: value };tocategories[categories.length] = { 'id': id, 'value': value };id: []andvalue: []properties into your object 'selected_value' and then you can useselected_value.id.push(value)andselected_value.value.push(value)