Upon reviewing your code below, I noticed that it's almost functional. To make it work, simply change id='div1' to class='hid' and follow the steps outlined below:
<a href="showhide(document.getElementById('div1'))">Show/hide div1</a>
<div id="div1" style="display:none;">.....</div>
Here is the solution to the problem. I encountered a similar issue, but managed to resolve it by referencing this helpful post and following the provided instructions.
Take a look at my own code snippet below, which functions as intended:
For my CSS styling, I used the following:
Next, I implemented a simple button to control the visibility of the content:
<button id="see" type="button" onclick="clickSee();">Show More ...</button>
Below is the Javascript code I used, utilizing JQuery 3.2.1 to change the button's function for toggling content visibility:
If you haven't used JQuery before, simply include the following script in the <head></head>
section:
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script>
function clickSee() {
document.getElementById("see").innerHTML = "... Show Less";
var divs = document.getElementsByClassName("hid");
for (var i = 0; i < divs.length; i++){
divs[i].style.display = "block";
}
//I use JQuery
$("#see").attr("onclick", "clickHide()");
}
function clickHide() {
document.getElementById("see").innerHTML = "Show More ... ";
var divs = document.getElementsByClassName("hid");
for (var i = 0; i < divs.length; i++){
divs[i].style.display = "none";
}
//I use JQuery
$("#see").attr("onclick", "clickSee()");
}
</script>
If you encounter any issues, feel free to leave a comment below for assistance. I'm here to help you until the problem is resolved.
There are various approaches to solving this issue, but the method I've shared is a fundamental one that worked in my case.
Open to any new recommendations or suggestions. Thank you.