简体   繁体   中英

How to add variable to a href

I want to do something like this

var myname = req.session.name;              <------- dynamic
<a href="/upload?name=" + myname class="btn btn-info btn-md">

But this does not work. So how do I properly pass in a dynamic variable to href? <a href="/upload?name=" + req.session.name class="btn btn-info btn-md"> does not work either

Actually there's no way to add a js variable strictly inside DOM. I would suggest you to apply an id attribute to that a element, refer to it and apply given variable as a new href attribute.

 var elem = document.getElementById('a'), myname = 'req.session.name'; //used it as a string, just for test cases elem.href += myname; console.log(elem.href); 
 <a id='a' href="/upload?name=" class="btn btn-info btn-md">Link</a> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM