How to open a link with the text of a div

Asked

Viewed 72 times

0

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

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

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

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

How do I do that? This one doesn’t work.

  • Doesn’t it work? What happens? How did you define button2? And what is the value in div? By the way, what is the structure of HTML?

  • I’m asking how I do it I just gave a silly example

  • the link that the button opens when you click on it is an element that I put on the page, for example create a div with a url and I want the button link to be the text of the same.

  • From what you’ve described, your example comes very close to doing what you want. Try to implement based on your example and, if it doesn’t work, edit your question with something more concrete.

1 answer

0


You can take the text of the element and put it in the first parameter of the window.open which 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');
}

Testing in the Jsfiddle

  • almost that friend,I want the link that is opened instead of containing the text the url of it is the text

  • <div id="test"> www.google.com <br /> <Strong>Hello! </Strong> </div> <button>Open</button>>

  • @Minelopsml Updated response. See if this was it?

  • it did, thank you very much! ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.