How to open a link with the text of a div

0

I want to create a button and the link to open it is a div that is in my site for example in JavaCcript

var elemento = document.getElementById('teste').innerHTML;

button2.onclick = function() {
 window.open(elemento)
}

var elemento receives the URL and when I click the button it opens the url of the ('teste').innerHTML element

How do I do this? This does not work.

    
asked by anonymous 12.01.2018 / 21:24

1 answer

0

You can get the text of the element and put it in the first parameter of window.open that refers to the URL that will open in the window:

HTML

<div id="teste">
    http://url_foo.com
</div>

<button>Abrir</button>

JS

button2 = document.querySelector("button");
button2.onclick = function() {
   var elemento = document.querySelector("#teste").innerText.trim();
   var popup = window.open(elemento,'_blank','width=500, height=300');
}

JSFiddle test

    
12.01.2018 / 22:57