tags:

views:

22

answers:

4

Hello,

I have two select fields and I want to compare them so if they both equal United States then alert the user.

Here is what I have but it doesn't seem to work.

if(document.getElementById('country_o').value == "United States" AND document.getElementById('country_d').value == "United States") {
    window.alert("BOTH US!");
    }

Thanks!

+1  A: 

Is it like this?

if(document.getElementById('country_o').value == "United States" && document.getElementById('country_d').value == "United States") { window.alert("BOTH US!"); }

where AND is replaced by &&

Nicholas Mayne
+1  A: 

The AND operator is &&.

And to get the selected element on a SELECT element use the index:

document.getElementById('country_o')
    .options[document.getElementById('country_o').selectedIndex].value
BrunoLM
A: 

Just a guess, but I think it should be && not AND. If you're getting a script error, that's likely your problem. Note that & would probably also work, but I believe && is the logical and & is bitwise.

Kendrick
A: 
(function() {
    var select1 = document.getElementById("country_o");
    var select2 = document.getElementById("country_d");
    if (select1.value === select2.value === "United States") {
        // alert user
    }
})();
Šime Vidas