1

I have 3 dropdowns, I would like to check in JavaScript that at least one selection has been made, if not display a message. What would be the simplest way to do it in JavaScript?

id: dropdown1 id: dropdown2 id: dropdown3

Thanks

1 Answer 1

1

// need to make sure the first dropdown option is blank, then...

var dd1 = document.getElementById('dropdown1');
var dd2 = document.getElementById('dropdown2');
var dd3 = document.getElementById('dropdown3');

if (
  (dd1.selectedIndex == 0) && 
  (dd2.selectedIndex == 0) && 
  (dd3.selectedIndex == 0)
) {  
  alert('need to select at least one')
}
Sign up to request clarification or add additional context in comments.

2 Comments

or if(!dd1.selectedIndex || !dd2.selectedIndex || !dd3.selectedIndex)
@mkoryak: Need to use '&&' between the conditions, or remove the extra '!' before the dd2 & dd3. ...but you got me thinking that the test for null doesn't work anyway - need to do something like make the first option a blank, and test to see if the index >0. I'm revising my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.