-1

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
    }
1

9 Answers 9

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.

Sign up to request clarification or add additional context in comments.

Comments

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
}

Comments

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).

2 Comments

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.
@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
0
var myValue = document.getElementById("x").value;

if(myValue == 2 || myValue == 3) {
    //...
}

Comments

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])) {

Comments

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>

Comments

0

try this

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

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

Comments

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
}

Comments

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) 

1 Comment

This will always fail, because element values are strings.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.