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!
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 &&
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
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.
(function() {
var select1 = document.getElementById("country_o");
var select2 = document.getElementById("country_d");
if (select1.value === select2.value === "United States") {
// alert user
}
})();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.