I need something like this:
var link = " http://www.google.com";
<a href='link' />
So I need to use a variable as href attribue of a link. Is this possible?
It worked with "<a href= '" + link + "' />". Is there a better way?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You should set the href value on the server, it is bad design to use scripting for this.
However, to answer the question, it can be done by using script to set the href property of the A element once it is in the DOM, e.g.:
<a href="<a useful URI if javascript not available>" id="a0">foo</a> <script type="text/javascript"> var a = document.getElementById('a0'); if (a) { a.href = "http://www.google.com"; } </script>Of course there are a thousand other ways, all with their pitfalls. So set the value on the server and avoid them all.