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

display a div based on current anchor

Status
Not open for further replies.

bcoates84

Programmer
Joined
Jan 27, 2006
Messages
1
Location
GB
What I'm trying to achieve is this:

By appending an anchor to the end of the URL
(eg. test.htm#anchor1)
I want javascript then to display ONLY the div on the page with the corresponding ID value 'anchor1'

I've tried the following code, but I'm getting errors:

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

<html>

<head>

<script language="javascript">

<!--
pageAnchor = window.location.search;
function displayAnchor(anchor) {
if (anchor.style.display == "") anchor.style.display = "none";
else anchor.style.display == "";
return false;
}
-->

</script>


</head>

<body onload="return displayAnchor(pageAnchor);">

<div id="anchor1" style="display: none; background-color: red;">

test anchor

</div>

<div id="anchor2" style="display: none; background-color: blue;">

test anchor 2

</div>

</body>

</html>
 
One immediate thing to change... this:

Code:
pageAnchor = window.location.search;

should be this:

Code:
pageAnchor = window.location.[!]hash[/!];

At present, you're not even picking up on the anchor.

Hope this helps,
Dan




[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
You should add this as the very first line of your function:

Code:
anchor = document.getElementById(anchor);

You should also put in place some basic error checking so the code doesn't fail if there is no anchor passed in.

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top