0

In Jquery how can i compare two array values. Below is my script

var corrAns = ['chBox_2','chBox_4','chBox_6','chBox_7','chBox_9'];
var userAns = new Array();

$("input:checkbox").change(function() {
        var someObj = {};
        someObj.userAns = [];

        $("input:checkbox").each(function() {
            if ($(this).is(":checked")) {
                someObj.userAns.push($(this).attr("id"));
            } else {

            }
        });

        alert( someObj.userAns);

    });

Thanks, Sankar

1
  • Create a fiddle to get quick help but try jQuery inArray Commented Nov 26, 2013 at 11:02

2 Answers 2

1

To see if 2 simple arrays (containings strings or numbers, not objects) are identical you can do:

array1.sort().toString() === array2.sort().toString()
Sign up to request clarification or add additional context in comments.

Comments

0

DEMO

var corrAns = ['chBox_2', 'chBox_4', 'chBox_6', 'chBox_7', 'chBox_9'];

$("input:checkbox").change(function () {
    var userAnswers = $(":checkbox:checked").map(function () {
        return this.id
    }).get().join(',');

    if (userAnswers === corrAns.join(',')) alert('Correct Answers!');

});

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.