Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Put a value in an href address of an anchor when user clicks on it 1

Status
Not open for further replies.

ericnet

Programmer
Joined
Mar 29, 2006
Messages
106
Hello,
Is there a way to put the current value of a hidden field in this link (where X is) when user clicks on the link?

Code:
<a id="my_link" href="details_page.htm?[b]Page_point=X[/b]">Go to details page</a>


Code:
<html>
<head>
<title>Test Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<body>
<form id="js_test">
  <input type="hidden" id="point">
  <a id="my_link" href="details_page.htm?[b]Page_point=hidden field value[/b]">Go to details page</a>

</form>
</body>
</html>

Thanks
 
Code:
<html>
<head>
<title>Test Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
[!]<script type="text/javascript">

function setValue(obj) {
   var x = document.getElementById("point").value;
   obj.href = "details_page.htm?Page_point=" + x;
   return true
}

</script>[/!]
</head>

<body>
<form id="js_test">
  <input type="hidden" id="point">
  <a id="my_link" href="details_page.htm" [!]onclick="setValue(this)"[/!]>Go to details page</a>

</form>
</body>
</html>

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Only 2 things I'd like to add to kaht's excellent post here:[ol]
[li]I would want to add a semi-colon "[tt];[/tt]" after both "[tt] return true;[/tt]" and "[tt]onclick="setValue(this);"[/tt]". Although this may not be necessary, it may solve some future browser issues before they start...makes the syntax slightly cleaner.[/li]
[li]It would be very tempting (to me) to try using an [tt]onMouseOver="setValue(this);"[/tt] in place of [tt]onClick="...;"[/tt], although this, too, is probably unnecessary (by syntax, the JavaScript in [tt]onClick="...;"[/tt] should be processed before the HTML gets to process the [tt]href[/tt]), but why not be safe, and give the browser a couple of extra tenths of a second to prep the [tt]href[/tt] attribute before the user clicks on the link?[/li][/ol]




I hope this helps;
Rob Hercules
 
Yes, it works fine! Thank you very much, I thought that it wasn' t possible

Which way is better, your' s or this one? :
Code:
<form id="js_test">
  <input type="hidden" id="point">
  <a href="details_page.htm" onclick="this.href += '?Page_point=' + document.js_test.point.value; return true;">Go to details page</a>

</form>
 
That works too, and keeps it nicely fit into one line. I'd use your alteration.

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top