How to use href links inside an option tag with JavaScript?

Spread the love

Sometimes, we want to use href links inside an option tag with JavaScript.

In this article, we’ll look at how to use href links inside an option tag with JavaScript.

How to use href links inside an option tag with JavaScript?

To use href links inside an option tag with JavaScript, we set the window.location property when we select an option.

For instance, we write

<select name="formal" onchange="javascript:handleSelect(this)">
  <option value="home">Home</option>
  <option value="contact">Contact</option>
</select>

to add the select drop down.

Then we write

handleSelect = (elm) => {
  window.location = elm.value + ".php";
};

to define the handleSelect function.

In it, we set window.location to the location we want to navigate to when the select option changes.

Conclusion

To use href links inside an option tag with JavaScript, we set the window.location property when we select an option.

Leave a Reply

Your email address will not be published.