Click to See Complete Forum and Search --> : Need help on jumping to Anchors...


Theone03
02-02-2004, 12:34 PM
I am a newbie on Javascript and could really appreciate some help on this one. I am trying to create something which users can type a two letter anchor name ("<a name='ha'>") for example and jump down to that part of a large page.

I have been messing around for a while but cannot get it to work. The only way i can do it is this:

<html>
<head>

<title> Anchor Test </title>

<script language="Javascript">
<!--

var question = prompt("Type the first two letters of the post code you want:","HA");

// -->
</script>
</head>


<body>


<script language="Javascript">
<!--

document.writeln("<a href=#" + question + ">click here</a> to jump to " + question);

// -->
</script><BR><BR>



<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>








<a name="HA">

it works...

</body>
</html>

------------------------------------------------------

It does work but seems stupid to type the two letters in then click the link after, Also I would rather the user type the letters into a form on the page and click a submit button than use the Prompt method.

Anyone have any ideas? Help really appreciated.

Thanks

Steve

:confused:

gil davis
02-02-2004, 12:37 PM
window.location.hash = question;

96turnerri
02-02-2004, 12:41 PM
<html>
<head>
<title> Anchor Test </title>

<script language="Javascript">
function go() {
document.writeln("<a href=#" + question + ">click here</a> to jump to " + question);
</script>
</head>

<body>
<form name="form">Enter First Two Letters Of Your Post Code&nbsp;
<input type="text" name="text" size="5">&nbsp;<input type="button" value="Go" onclick="go();">
</form>

<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<a name="HA">

it works...

</body>
</html>

Theone03
02-02-2004, 05:43 PM
Originally posted by 96turnerri
<html>
<head>
<title> Anchor Test </title>

<script language="Javascript">
function go() {
document.writeln("<a href=#" + question + ">click here</a> to jump to " + question);
</script>
</head>

<body>
<form name="form">Enter First Two Letters Of Your Post Code&nbsp;
<input type="text" name="text" size="5">&nbsp;<input type="button" value="Go" onclick="go();">
</form>

<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<a name="HA">

it works...

</body>
</html>
Thanks for trying 96turnerri (Rich).

However this method doesn't seem to work, It is basically (After a bit tweeking) creating a link when you submit the thing. What I want it to do is go straight to the Anchor.

Any more suggestions.

This is the exact script I have now;

--------------------------------
<html>
<head>
<title> Anchor Test </title>


<script language="Javascript">
<!--



// -->
</script>



<script language="Javascript">
function go() {
var question = form.text.value;
document.writeln("<a href=#" + question + ">click here</a> to jump to " + question);
}
</script>
</head>







<body>
<form name="form">Enter First Two Letters Of Your Post Code
<input type="text" name="text" size="5"> <input type="button" value="Go" onclick="go();">
</form>

<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>

<a name="HA">

it works...

</body>
</html>

--------------------------------

try it on a blank notepad file, Lets see if anyone can actually make it jump straight down.

Thanks

Steve...
:)

Theone03
02-02-2004, 05:56 PM
Originally posted by gil davis
window.location.hash = question;

Thanks a lot pal.................. It works like a dream sorry about double posting but I basically read your message second.

In case anyone wants to know this was what I ended up with...

<html>
<head>
<title> Anchor Test </title>


<script language="Javascript">


function go()
{
var question = form.text.value;
window.location.hash = question;
}


</script>
</head>







<body>
<form name="form">Enter First Two Letters Of Your Post Code
<input type="text" name="text" size="5"> <input type="button" value="Go" onclick="go();">
</form>

<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>

<a name="HA">

it works...

</body>
</html>

Theone03
02-02-2004, 07:17 PM
I have been messing around with the form and am having trouble now getting the thing to call the function when enter is pressed in the form.

The submit button works like a dream but when it comes to pressing enter (return) then it submits the form after calling the function. so you end up with a url with text=HA at the end of it.

I know I am probably asking a daft question here that really is annoying you but I have been scouring the net and couldn't find what I'm looking for.

Any help appreciated

Thanks
Steve...:confused:

gil davis
02-02-2004, 08:30 PM
Try this link:
http://www.faqts.com/knowledge-base/view.phtml/aid/1225/fid/130