0

I have a dropdown menu that I want people to click and redirect to another page.

This is my HTML code:

<!DOCTYPE html>
<html>
      <select id="searchengine" name="searchengine" class="searchengineSelector">
        <option value="">Select an option</option>
        <option value="GO">Google</option>
        <option value="YA">Yahoo</option>
      </select>
      <div>
        <button class="searchSelect" id="search_button">Explore</button>
      </div>
</html>
<script>
  const links = {
  GO: 'https://www.google.com',
  YA: 'https://www.yahoo.com',
  }
  document.getElementById('search_button').addEventListener('click', () =>
    location.href = links[document.getElementById('searchengine').value])
</script>

Using this script everything works but I want it to open in a new tab. How can I do that? I tried adding a '_blank' next to the link but it doesn't work.

Thank you!

motspeed
  • 37
  • 6
  • Does this answer your question? [Open a URL in a new tab (and not a new window)](https://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window) – ericmp Nov 07 '22 at 16:49

1 Answers1

-1

You can do this:

window.open('https://link.com', '_blank');
  • See also [target attribute in a elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attr-target) and [window.open](https://developer.mozilla.org/en-US/docs/Web/API/Window/open) – kil Nov 07 '22 at 16:51