Reputation: 71
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
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