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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Go to Anchor Function 1

Status
Not open for further replies.

JBorges

Technical User
Jan 12, 2009
57
0
0
DK
Hi!

I have this code below that let's me type in a number that goes to the requested anchor tag. All my anchor tags in my html file are numbers.

<input type="text" onchange="window.location ='#' + this.value;" />

I would like to make this a function. I tried the following but it doesn't work:

<script>
function go() {
window.location ='#' + this.value;
}
</script>

<body style="font-size: 14px;">

<input type="text" onchange="go();" />

Can anyone help me?
--Philip
 
As I posted in the HTML forum:

With one chane:

Your function doesn't know what "this" is. As "this" would be itself.

You need to pass the reference to it like this:

Code:
onChange="myfunction(this);"


...


And your function definition should look like this.

function myfunction(mybox){
window.location ='#' + mybox.value;

}
You may also want to rename your function as go is a Javascript function already, and it may cause problems




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Phillip,
The issue seems to be the reference to THIS in the function refers to the WINDOW object - add an
Code:
alert(this)
in FF rather than the element that is passing to the function, I've reworked your code a little and have tested in FF and the following seems to work with minimal changes :

Code:
<html>
	<head>
		<script>
			function go(ref)
			{
				alert(ref);
			    window.location ='#' + ref;
			}
		</script>
	</head>
	<body style="font-size: 14px;">
		<input type="text" onblur="go(this.value);" />
		<p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p>
		<p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p><p>&nbsp;<p>
		<a name="one">this is one</a>
	</body>
<html>

This looks like the best way to do it as per
Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
Thanks Greg for your help. The help vacunita provided works perfectly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top