Collapsible button doesn’t…collapse!
-
Hi, I’d like to add to my (localhost) site a collapsible button with the following features (here you can see it in action):
- button width has not to be 100%, but has to adapt to the text inside the button
- button can be inserted in a line of text without forcing the words after the button to go to the next line (I tried the plugin collapse-o-matic but the words after the button, even if they are written in the same line, are forced to go to the next line)
I was looking for how to manually (without plugins) create the button, and I ended up to a w3schools page where, using the online editor, I created the button I want (see link above) whose code is the following
<!DOCTYPE html> <html> <head> <style> .collapsible { background-color: #777; color: white; cursor: pointer; border: none; } .ccontent { padding: 0 18px; max-height: 0; overflow: hidden; transition: .3s ease-out; background-color: #f1f1f1; box-shadow: 5px 5px 10px 3px inset rgba(0,0,0,0.60); } </style> </head> <body> Does <button class="collapsible">this</button> works? <div class="ccontent"><p>Yes!</p></div> Good job! <script type="text/javascript"> var coll = document.getElementsByClassName("collapsible"); var i; for (i = 0; i < coll.length; i++) { coll[i].addEventListener("click", function() { this.classList.toggle("active"); var content = this.nextElementSibling; if (content.style.maxHeight){ content.style.maxHeight = null; } else { content.style.maxHeight = content.scrollHeight + "px"; } }); } </script> </body> </html>
Next I tried to figure out how to add the code to wordpress, so this is what I did
- added .collapsible and .content to the style.css file
- added the javascript to the footer using a plugin
then in a post I wrote the following code
Does <button class="collapsible">this</button> works? <div class="ccontent"><p>Yes!</p></div> Good job!
but, while the button is correctly showing, when clicking on it nothing happens. Since the button is showing, I think the problem has to do with the javascript code.
This is what it shown in my post, as you can see another problem is the blank space between the first line and the second.I’m almost sure that the javascript is loaded because when I open the page source (CTRL+U) I see that, some line before the </body> tag, the script is shown.
- The topic ‘Collapsible button doesn’t…collapse!’ is closed to new replies.