0

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

4
  • Can you provide a jsFiddle showing the problem? Commented Feb 18, 2014 at 15:23
  • You should probably have to change: categories[categories.length] = { id: value }; to categories[categories.length] = { 'id': id, 'value': value }; Commented Feb 18, 2014 at 15:29
  • and how i will add those values to my var selected_value? that is my main issue Commented Feb 18, 2014 at 15:31
  • First of all you must add id: [] and value: [] properties into your object 'selected_value' and then you can use selected_value.id.push(value) and selected_value.value.push(value) Commented Feb 18, 2014 at 15:35

1 Answer 1

2

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;
});
Sign up to request clarification or add additional context in comments.

6 Comments

i get error in this line: categories.push(obj); // obj is not defined
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
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?
if you provide a jsFiddle I'll try to sort it out for you
Can you check if the categories array has data?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.