user930453
user930453

Reputation: 71

comparing select option value

By using the following code I am able to get the matched items. How to find the unmatched items from this:

$('#list1 option').each(function (i, option)                        
    $('#list2 option').each(function (j, option) {
       if ($('#list1 option').val() ==$('#list2 option').val())
           matchedList= $('#list1 option').val()
       });
    });
});

Upvotes: 1

Views: 2088

Answers (1)

Rob W
Rob W

Reputation: 348982

The second argument of the each loop callback holds a reference to the HTML element, HTMLOptionElement, in this case. Simply use the value property to compare the values.

Use a !== to negate the comparison.

var nonMatchedList = [];
$('#list1 option').each(function (i, option1) {
    $('#list2 option').each(function (j, option2) {
       if (option1.value !== option2.value) {
           nonMatchedList.push(option1.value);  // Add to list
           return false;                        // Stop looping through list2
       }
    });
});

Upvotes: 2

Related Questions