- First option is the default, so the text of the options from other options is retrieved and compared.
- Now, after displaying an alert message, I want to clear the option selected in No 2 and 3 but not No. 1 which is the default.
Link https://jsfiddle.net/fswygznq/1/
function check(){
var $fruits = [];
$("select.fruits").each(function() {
var $two = $(this).find("option:selected").text();
$fruits.push($two)
})
for ($i= 1; $i < $fruits.length; $i++) {
for ($j= 0; $j < 5; $j++) {
if ($fruits[$i] == "Apple" && $fruits[$i+$j] == "Banana") {
alert ("Since you have selected Apple, you cannot select Banana");
$("select.fruits").val(""); // this one clears all the options including the default**
return false;
}
if ($fruits[$i] == "Banana" && $fruits[$i+$j] == "Apple") {
alert ("Since you have selected Banana, you cannot select Apple");
$("select.fruits").val(""); // this one clears all the options including the default
return false;
}
}
}
}