How to get a javascript value in a href


I have js function that returns a value that contains the path of a document.  I want to pass it in a href link on the same page.  Upon clicking the link, user needs to go that document.  Pls let me know how I can achieve that.

Who is Participating?
ZvonkoConnect With a Mentor Systems architectCommented:
Sorry, I missread your question.
So you already have a function that returns the string which is the path to some document.
Then your link can be like this:

<a href="#" onClick="this.href=yourFunction()">Document</a>

ZvonkoSystems architectCommented:
The links are contained in a collection of document links.
So if you want to change some link's href after some script calculation, then you can do it like this:

function addPath(lnkNum, thePath){
  var lnk = document.links;
  lnk[lnkNum].href += ((lnk[lnkNum].href.indexOf("?")>0)?"&":"?")+"path="+thePath;

radha77Author Commented:

Its not working.  Can you tell me how my a href in my form should look like??  

radha77Author Commented:
It works now.  Thank you :-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.