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,295
  • 8
  • 49
  • 69
Dimitar Spasovski
  • 1,898
  • 6
  • 28
  • 39

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,295
  • 8
  • 49
  • 69
1

JS Fiddle

function openwindow() {
    var a = document.getElementById("a1").href;
    window.open(a, '_blank');
}
Vitorino fernandes
  • 15,429
  • 2
  • 19
  • 37