Home > Blockchain >  How do I set the URL of a hyperlink to a js variable?
How do I set the URL of a hyperlink to a js variable?

Time:01-14

I need help with setting urls to JS variables,

<input id="urlbox"><a href="main.htm" target=main id=gourl><button>Go</button></a>
<script>
  var urlgo = document.getElementById('gourl');
  var seturl = document.getElementById('urlbox').value;
  setInterval(function() {
  urlgo.setAttribute('href', seturl);
}, 300);
</script>

It doesn't work in any internet browser I tested it in, how do I get it to work?
The behavior I expected is that every 0.3 seconds, the url of the go button is set to the value of the text box, but nothing happens when i use it, does anyone know how to make it work?
If it helps, I don't get an error, but the url just gets set to blank no matter what I enter

CodePudding user response:

Place the

var urlgo = document.getElementById('gourl');
var seturl = document.getElementById('urlbox').value;

inside the the setInterval.

  •  Tags:  
  • Related