Home > Software engineering >  Is there any way I can navigate to a link using options in html using JavaScript?
Is there any way I can navigate to a link using options in html using JavaScript?

Time:06-01

I created a list of options using html and placed a button beneath it...I want the user to select an option and when he clicks on the button,it redirects him to a different page... Is there anyway that I can do this using JavaScript please?

CodePudding user response:

Yes, window.location.href = "/someLocation" just is made for you.

CodePudding user response:

when you click in the button you can call a function and inside of this function you put window.location.href = "your-url" or you can use window.open("your-url") to open a new tab.

<select onChange="goToPage(value)">
  <option disabled selected value> -- select an option -- </option>
  <option value="https://google.com" > google </option>
  <option value="https://stackoverflow.com" > stackOverflw </option>
  <option value="https://github.com" > git </option>
</select> 
 
<script>
  function goToPage(value) {
    window.location.href = value
  }
</script>

you an see more in this link here

  • Related