Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Comparing SINGLE Value Against MULTIPLE Values with OR Operands [duplicate]

Possible Duplicate:
Check variable equality against a list of values
Javascript if statement with multiple permissible conditions

I must click the same 21 of 253 items (li) in a dropdown list (ul).

Scrolling I'll have to do this for the same list on 500+ pages, I figured I could Javascript inject each ul, loop through and click each li which happens to be one of the 21. It seems I cannot do something like

 if(item[i] === ('aasdf'|'basdf'|'cwefw'|'asdfd'|'trehe'|'ferth'|'erthg'|'erthh'|'ierth'|'jeth'|'kerth'|'lerth'|'merth'|'psdfg'|'gregq'|'rsrgs'|'sress'|'srget'|'sergu'|'sdfgsv'))

Is there a syntactically cleaner way of writing this ugly if statement below?

var item = document.getElementById('myDropdownList').getElementsByTagName('li');

for (i=0;i<item.length;i++){

    if(item[i].innerText === 'Argentina' | item[i].innerText === 'Australia' | item[i].innerText === 'Brazil' | item[i].innerText === 'Canada' | item[i].innerText === 'China' | item[i].innerText === 'Colombia' | item[i].innerText === 'France' | item[i].innerText === 'Germany' | item[i].innerText === 'Indonesia' | item[i].innerText === 'India' | item[i].innerText === 'Italy' | item[i].innerText === 'Japan' | item[i].innerText === 'Malaysia' | item[i].innerText === 'Mexico' | item[i].innerText === 'Philippines' | item[i].innerText === 'Russia' | item[i].innerText === 'South Africa' | item[i].innerText === 'Sweden' | item[i].innerText === 'Switzerland' | item[i].innerText === 'United Kingdom' | item[i].innerText === 'USA'){

    item[i].click();

    }

}
like image 283
AlecPerkey Avatar asked Dec 19 '12 18:12

AlecPerkey


People also ask

What is == and === in JavaScript?

== in JavaScript is used for comparing two variables, but it ignores the datatype of variable. === is used for comparing two variables, but this operator also checks datatype and compares two values. Checks the equality of two operands without considering their type. Compares equality of two operands with their types.

What is === in JavaScript?

The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.

How do you compare multiple values in an if statement?

To check if a variable is equal to all of multiple values, use the logical AND (&&) operator to chain multiple equality comparisons. If all comparisons return true , all values are equal to the variable. Copied! We used the logical AND (&&) operator to chain multiple equality checks.

How do I compare 3 numbers in JavaScript?

To compare 3 values, use the logical AND (&&) operator to chain multiple conditions. When using the logical AND (&&) operator, all conditions have to return a truthy value for the if block to run. Copied!


2 Answers

You could use a variable and multiple comparison, but that's still lengthy:

var text = item[i].innerText;
if (text === 'Argentina' | text === 'Australia' | text === 'Brazil' | text === 'Canada' | text === 'China' | text === 'Colombia' | text === 'France' | text === 'Germany' | text === 'Indonesia' | text === 'India' | text === 'Italy' | text === 'Japan' | text === 'Malaysia' | text === 'Mexico' | text === 'Philippines' | text === 'Russia' | text === 'South Africa' | text === 'Sweden' | text === 'Switzerland' | text === 'United Kingdom' | text === 'USA')

Or you could just use an array and check if the string is contained in it.

var matches = ['Argentina','Australia','Brazil','Canada','China','Colombia','France','Germany','Indonesia','India','Italy','Japan','Malaysia','Mexico','Philippines','Russia','South Africa','Sweden','Switzerland','United Kingdom','USA'];
if (~ matches.indexOf(item[i].innerText) …

Yet, for the complicated != -1 comparison and the lack of native indexOf in older IEs, people tend to use regexes:

var regex = /Argentina|Australia|Brazil|Canada|China|Colombia|France|Germany|Indonesia|India|Italy|Japan|Malaysia|Mexico|Philippines|Russia|South Africa|Sweden|Switzerland|United Kingdom|USA/
if (regex.test(item[i].innerText)) …
like image 128
Bergi Avatar answered Nov 14 '22 23:11

Bergi


var options = ['Argentina', 'Australia', 'Brazil', 'Canada', ...];
if (options.indexOf(item[i].innerText) !== -1){
  // item[i] was found in options
}

Something like that? use Array.indexOf (Unless I've mis-read the question? In which case post a comment and I'll do my best to re-work my answer)

like image 23
Brad Christie Avatar answered Nov 14 '22 21:11

Brad Christie