1

I'm trying to launch a command based on a select dropdown value. If the value is Alabama or Alaska then the page should re-direct to another webpage. If the value is California then the page is unchanged.

I don't want to check the value onchange, rather it should be when the user clicks on the button after the dropdown.

Here's the jsfiddle for reference: https://jsfiddle.net/41qpawr8/1/

I think this can be done in Javascript or JQuery but I don't them well enough to proceed. Thanks in advance :)

<select class="select-state" name="state" id="administrative_area_level_1">
          <option value="">-</option>
          <option value="AL">Alabama</option>
          <option value="AK">Alaska</option>
          <option value="CA">California</option>
</select>

<button id="check">
Check
</button>

1 Answer 1

2

Add a click event listener on the button that checks whether the value of the select is either AL or AK, and if it is, we can redirect by assigning a url to window.location:

const select = document.querySelector('select');
const locationToRedirectTo = "https://stacksnippets.net";
check.addEventListener('click', function() {
  if (select.value == "AL" || select.value == "AK") {
    location = locationToRedirectTo;
  }
})
<select class="select-state" name="state" id="administrative_area_level_1">
  <option value="">-</option>
  <option value="AL">Alabama</option>
  <option value="AK">Alaska</option>
  <option value="CA">California</option>
</select>

<button id="check">
Check
</button>

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

Comments

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.