0

I have a simple HTML/javascript question. I have a button and a link. My HTML looks like this :

<a id="a1" href="https://www.facebook.com/"> </a>
<input type="button">

How can I open the link by clicking the button? I know I can just type a function in javascript like

function f1() {
    location = document.getElementById("a1").href;
}

and put it in the onclick attribute of the button but I want the link to be opened in a new tab. Can anyone tell me how to do this via javaScript/HTML?

Alex Char
  • 32,879
  • 9
  • 49
  • 70
Dimitar Spasovski
  • 2,023
  • 9
  • 29
  • 45

2 Answers2

2

You can try with window.open:

function f1() {
    window.open(document.getElementById("a1").href, "_blank");
}

fiddle

Reference

window.open

Alex Char
  • 32,879
  • 9
  • 49
  • 70
1

JS Fiddle

function openwindow() {
    var a = document.getElementById("a1").href;
    window.open(a, '_blank');
}
Vitorino fernandes
  • 15,794
  • 3
  • 20
  • 39