/    Sign up×
Community /Pin to ProfileBookmark

Need help on jumping to Anchors…

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

?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisFeb 02.2004 — window.location.hash = question;
Copy linkTweet thisAlerts:
@96turnerriFeb 02.2004 — <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>
Copy linkTweet thisAlerts:
@Theone03authorFeb 02.2004 — [i]Originally posted by 96turnerri [/i]

[B]<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> [/B]
[/QUOTE]

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...

?
Copy linkTweet thisAlerts:
@Theone03authorFeb 02.2004 — [i]Originally posted by gil davis [/i]

[B]window.location.hash = question; [/B][/QUOTE]


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>
Copy linkTweet thisAlerts:
@Theone03authorFeb 03.2004 — 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...?
×

Success!

Help @Theone03 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...