I'm trying to make a href link to an anchor on another page (let's call it page 2 to make it easier), however on page 2, the anchor in question is contained within a div that is originally hidden when you first visit it (The div expands to reveal the content when header is clicked. So of course by default, the div has the property 'display:none').
Here's a small sample of the exact kind of thing I'm talking about.
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<style>
p {color:#000;}
</style>
<script>
$(document).ready(function(){
$("#f-hdr").click(function(){
$("#hidden").toggle(1000);
});
});
</script>
</head>
<body>
<a href="#anch">Link to anchor within hidden div</a>
<h2><a id="f-hdr" href="javascript:void(0)">Div Header</a></h2>
<div id="hidden" style="display:none">
<p id="anch">some text</p>
</div>
</body>
</html>
Thank you!
EDIT - For instance, say if I was on 'page 1', I want to be able to open a link to the specific element on 'page 2' (in this case, #anch) But '#anch' is contained in a div (#hidden) which is hidden when the page is first opened. Originally, to access the content in '#hidden', the user must click the heading, which expands the div to reveal the content. I would like to be able to click the link to '#anch' (which takes me to 'page 2') and view the content in '#anch' without clicking the heading first.
From what I have seen, it may have something to do with the 'onhashchange' event. Although I'm not too sure (hence the post) :)
The solution is that you need to use
visibility: hidden
instead ofdisplay: none
.I mean you need to change this:
By this:
For more information you can check this question What is the difference between visibility:hidden and display:none?
I'm posting the update with a long text added to it to see how it goes to the hidden element when you click on "Link to anchor within hidden div":
There are two ways of accomplishing this one using the name anchor attribute as suggested by W3schools.
W3schools: https://www.w3schools.com/tags/att_a_name.asp
And another approach could be to use generic jquery method which behaves like name anchor.
HTML:
JQuery:
Note: Here nameLink is the class which can be assigned to any anchor link for using same functionlity.
Finally the link:
https://jsfiddle.net/Ashokkumargupta/bc1drx0s/
Hope this solves your problem.
Thanks, Ashok