stockBoi
stockBoi

Reputation: 287

how to use jquery != with condtional || operator

Sorry, as this is same type question I asked today. But as the earlier question got bigger and confusing, I have to ask this question again separately. I have following code in my body section:

<form action="" method="post">
<select id="fruits" name="fruits">
<option>Apple</option>
<option>Mango</option>
<option>Orange</option>
</select>
<select id="color" name="color">
<option>Red</option>
<option>Green</option>
<option>Yellow</option>
</select>
<input type="text" id="result" value="" />

What I want to do, If a user select Apple from fruits and change color from second drop down menu, "Right Choice" will be appeared in #result input. On the other hand, if a user selects Mango or Orange from fruits and change color option, value will be cleared from #result input. So the jquery code will be like this:

$("#color").change(function() {
if($("#fruits").val()!="Mango" || $("#fruits").val()!="Orange"){
  $("#result").val(""); 
}else{
  $("#result").val("Right Choice"); 
}
});

But it is not working. Changing of any fruits, clearing the value from #result input. But if I use only one condition in if statement, it is working well.

$("#color").change(function() {
if($("#fruits").val()!="Mango"){
  $("#result").val(""); 
}else{
  $("#result").val("Right Choice"); 
}
});

Why this is happening?

Upvotes: 1

Views: 71

Answers (4)

Adil
Adil

Reputation: 148130

Change

if($("#fruits").val()!="Mango" || $("#fruits").val()!="Orange"){

To

if( !($("#fruits").val()=="Mango" || $("#fruits").val()=="Orange")){

Upvotes: 0

chandresh_cool
chandresh_cool

Reputation: 11830

Use this

  if(($("#fruits").val()!="Mango") && ($("#fruits").val()!="Orange")){
  $("#result").val(""); 
} else{
  $("#result").val("Right Choice"); 
}
});

Upvotes: 0

Raptor
Raptor

Reputation: 54212

if($("#fruits").val()!="Mango" || $("#fruits").val()!="Orange"){

means the case if TRUE if the value is either NOT Mango or NOT Orange. In your case, you should replace || with && so that if user selects Mango or Orange, it will prompt Right Choice.

Upvotes: 1

CaptainCarl
CaptainCarl

Reputation: 3489

If you want fruit to not be mango and orange you can use &&(AND) instead of OR.

Upvotes: 0

Related Questions