0

Is there anyway to do this:

    if (document.getElementById("x").value == 2 || document.getElementById("x").value == 3) {
         //Do somthing
    }

# Can I make it simple in some kind a way like this, I tried but it didn't work:

    if (document.getElementById("x").value == 2 || 3) {
         //Do somthing
    }
Mich'
  • 683
  • 1
  • 8
  • 17
  • possible duplicate of [Javascript: The prettiest way to compare one value against multiple values](http://stackoverflow.com/questions/9121395/javascript-the-prettiest-way-to-compare-one-value-against-multiple-values) – Felix Kling Apr 11 '13 at 14:01

9 Answers9

5

Just as an alternative to the other answers, if you don't want to be declaring another variable in that scope for whatever reason:

if (["2", "3"].indexOf(document.getElementById("x").value) > -1) {
    // Do something
}

Please note that older browsers don't support Array.prototype.indexOf so you would need to include a polyfill to handle that.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
2

The way you suggest does not work, as you've realized yourself. The || 3 part will always be truthy, and therefor the code within the if-statement would always run.

If you want to make the if-statement more readable, you could keep the value in a variable, and use the variable in the if-statement. Something like this:

var xVal = document.getElementById("x").value;
if (xVal == 2 || xVal == 3) {
     //Do somthing
}
Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
2

Something different:

if (document.getElementById("x").value in {2:0,3:0}) {
    //Do something
}

Personally I use this for such situations - it's often actually the shortest (especially as the 'ORs' pile up).

d'alar'cop
  • 2,357
  • 1
  • 14
  • 18
  • 1
    Original answer, but may be difficult to read/understand if you come back to it later, and would also get very messy if you want more values. – Niet the Dark Absol Apr 11 '13 at 14:01
  • @Kolink well, it depends what you're used to. I personally am comfortable with it and have been using it so.. it's as familiar as anything else.. Also more values is just: {2:0,3:0,4:0,5:0}.. etc. Cheers – d'alar'cop Apr 11 '13 at 14:03
0

you could get the value before hand, which would make it read clearer

var yourValue = document.getElementById("x").value

if (yourValue == 2 || yourValue == 3) 
dm03514
  • 54,664
  • 18
  • 108
  • 145
0
var myValue = document.getElementById("x").value;

if(myValue == 2 || myValue == 3) {
    //...
}
Wim Ombelets
  • 5,097
  • 3
  • 39
  • 55
0

With a modern browser:

if( [2,3].indexOf(document.getElementById('x').value) > -1)

Older versions of IE will need a shim to achieve this. Or you can implement it yourself:

function in_array(needle,haystack) {
    var l = haystack.length, i;
    for( i=0; i<l; i++) {
        if( haystack[i] == needle) return true;
    }
    return false;
}
// ...
if( in_array(document.getElementById('x').value,[2,3])) {
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

if you want to do something when the value is either 2 or 3 , you only have to write

if (document.getElementById("x").value == 2 || document.getElementById("x").value == 3) {
    //Do something
}
Wim Ombelets
  • 5,097
  • 3
  • 39
  • 55
0

Try like below it will help you

Fiddle : http://jsfiddle.net/RYh7U/129/

Script:

var value = document.getElementById("x").value;
if ((value in [,1,2]))
alert("Do SomeThing");

Full Script:

<html>
<head>
<script type='text/javascript'>

function check()
{
   var value = document.getElementById("txtin").value;
   if ((value in [,1,2,3]))
     alert("Do SomeThing");
}

</script>
</head>
<body>
    <a href="#" onclick='check()'>check</a>
    <input type="text" id="txtin"/>
</body>
</html>
Pandian
  • 8,848
  • 2
  • 23
  • 33
0

try this

var x=document.getElementById("x").value;

if (x == 2 || x == 3) {
         //Do somthing
    }