Solved

Javascript: Build dynamic URL

Posted on 2009-05-08
5
1,804 Views
Last Modified: 2013-11-19
Lets say in the same page I have::

<a href="javascript:;" onclick="somefunction();">23</a>

<a href="page.asp?number=">Enter</a>


How do I manipulate the href string in javscript so that 23 is inserted and URL becomes:

<a href="page.asp?number=23">Enter</a>
0
Comment
Question by:fcqmax
  • 3
5 Comments
 
LVL 1

Accepted Solution

by:
zacspam earned 500 total points
ID: 24337785
Well, to make it easier, I am going to add some id's to your code so we can grabt hem faster.  Then, you can use the function document.getElementById() to get a reference to the Enter anchor tag.  After that, you can set the .href attribute to whatever you want.
 
In the code below, I pass this.innerHTML to somefunction.  That will send whatever is between the <a> and </a> tags, which in this case is 23.  You could also change it to just send 23 on its own without being dynamic.  
In somefunction(), I get areference to the link we want to change and then set the href property to the link you wanted plus what was passed in.
 
Hope that helps!

<html><body>
<a href="javascript:;" onclick="somefunction(this.innerHTML);">23</a>
<a href="page.asp?number=" id="link">Enter</a>
 
<script type="text/javascript">
function somefunction(s) {
	document.getElementById("link").href="page.asp?number=" + s;
}
</script>
 
</body></html>

Open in new window

0
 

Author Comment

by:fcqmax
ID: 24341759
How about just appending a string? Is it possible to do something like:

document.getElementById("link").href = document.getElementById("link").href + s;


0
 

Author Comment

by:fcqmax
ID: 24341802
nvm, that works also. thanks
0
 

Author Closing Comment

by:fcqmax
ID: 31579514
Thanks.
0
 

Expert Comment

by:RJHarvey72
ID: 24411730
this code works for some of my asp pages. but what about pages that requiere multiple parameters to be passed.

can you use

<a href="page.asp?number=" id="link">Enter</a>

technique to pass more than one parameter tot he asp page?
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question