1

I have this function where when I click a button I loop over some divs and get the id of that div along with some checkbox inputs id's if they are marked. To store this values I have a dictionary which is suppose to store the items like this:

{
   1: [4,5],
   2: [2,3]
}

The keys represent the id's of each div and the values are the checkbox items id's that are clicked.

Full code:

$(document).on('click', '.trimite-rasp', function(){
    var formData = {}
    $.each($('.single-question'), function(){
        var listIds = [];
        var qId = $(this).attr('qid');
        console.log(qId);
        $.each($('.form-check-input'), function(){
            if($(this).is(':checked')){
                var thisId = ($(this).attr('aid'));
                $(listIds).push(thisId);
            }
        });

        formData[qId] = listIds;
    });
    console.log(formData);

});

The only problem here is that listIds array is always empty. I can't push the values to that array, and I'm sure the checkbox id is correct because I console.log it and it's taken correctly.

2
  • 2
    Lose the surrounding $() Commented Jan 30, 2018 at 16:34
  • If you had your Console tab of your developer's tools open, you'll most likely see an error something to the effect of push is not a method of JQuery. Commented Jan 30, 2018 at 16:35

2 Answers 2

3

It is because you are using the array as a jQuery selector

use

var thisId = $(this).attr('aid'); 
listIds.push(thisId);

instead of

 $(listIds).push(thisId);
Sign up to request clarification or add additional context in comments.

2 Comments

That was the problem. Thanks!
Happy to help :)
0

Try this

listIds.push(thisId);

Comments

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.