Let's say I have an Home Page that contains a link to a second page which has some kind of content, say a <details>
element. Now, the <details>
element in the second page is closed “by default” but I would like the link in the home page to redirect to the <details>
element and open it.
I would like to to this with basic html/css and javascript. Here is what I have so far:
home.html
<html>
<head>
<script type="text/javascript">
window.onload = function() {
var a = document.getElementById("mylink");
a.onclick = function() {
var b = document.getElementById("mydetails");
b.open = true;
b.style.color = "red"; // just to try another property
return false;
}
}
</script>
</head>
<body>
<h1>Home Page</h1>
<h2><a id="mylink" href="second_page.html#mydetails">Go to 2nd page</a></h2>
</body>
</html>
second_page.html
<html>
<body>
<h1>2nd page</h1>
<details id="mydetails" open="false">
<summary>Hello,</summary>
</details>
</body>
</html>
Unfortunately, the code runs but when I click on the link in the home, the <details>
in the second page doesn't get opened. How could I go about this?
Bonus points if I could pass as parameters to the JS function the ids of both the link and target element.
Related questions:
- How to use a link to call JavaScript?, from where I got most of my code
- Automatically open <details> element on ID call, this seems to be what I want to achieve, but I don't know how to get it to work