Reputation: 504
I have a JavaScript variable I grabbed from a form field and I am trying to use it for the href = value
in a link. What is the proper way to output this JavaScript variable in HTML?
Upvotes: 1
Views: 14565
Reputation: 3042
If you're not using jQuery, try this:
document.write('<a href="'+variable+'">Link to some site</a>');
Upvotes: -3
Reputation: 28625
This should be what you need.
var link = "http://www.google.com/";
var a = document.getElementById('yourlinkId');
a.href = link;
Upvotes: 7
Reputation: 35864
Update the href value via javascript. Using something like jQuery it is as simple as:
var link = "www.google.com";
$("a").attr("href", link);
Upvotes: 1