as it states in the title, my collapsible menu is not staying closed when the page is refreshed. Every time the page is loaded, the collapsible menu is fully expanded out, even if before the refresh it was completely closed. This is a bit of a problem, because there is a lot of stuff in this collapsible.
Here is basic code for it:
CSS:
//some code here for the design, background color and stuff that shouldn't matter,
// .active is what I think I need
.active, .collapsible:hover{
background-color: #02538D;
}
HTML:
<button class="collapsible">Tutorials</button>
<div>
<div class="content">
<p>
<?php
//some php here for output of collapsible
?>
</p>
</div>
<div class="content">
<p>
<?php>
//some php here for output of collapsible
?>
</p>
</div>
</div>
JavaScript:
<script>
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.display === "block") {
content.style.display = "none";
}
else {
content.style.display = "block";
}
});
}
</script>
I'm a beginner when it comes to JavaScript so I'm pretty sure that's where the error is but any help at all would be greatly appreciated. Thank you so much!
In the code you've provided, the state of the menu is never saved - so when the page is refreshed everything is simply 'reset' to the default.
One solution is of course to use 'display:none;' as the default in your css. That would make the menu hidden on page-refresh but the problem would persist if you need it to also stay visible between refreshes if the users have opened it.
In that case you could set a cookie with javascript at the time you toggle the styles:
HTML
<button class="collapsible">Collapsible 1</button>
<div>
<div class="menu-item">
<p>Content 1</p>
</div>
<div class="menu-item">
<p>Content 2</p>
</div>
</div>
JAVASCRIPT
var coll = document.getElementsByClassName("collapsible");
for(i = 0; i< coll.length; i++) {
var cookies = decodeURIComponent(document.cookie).split(";");
for(i=0;i<cookies.length;i++) {
if(cookies[i] == "menu-state=hide") {
var content = coll[i].nextElementSibling;
content.style.display = "none";
}
}
coll[i].addEventListener("click", function() {
var content = this.nextElementSibling;
if(content.style.display === "block") {
content.style.display = "none";
document.cookie = "menu-state=hide";
}
else {
content.style.display = "block";
document.cookie = "menu-state=display";
}
});
}
https://jsfiddle.net/hxcy1vLr/41/
The above code will check if there is a cookie with the name and value "menu-state=hide". If there is, the menu will be hidden initially. This cookie is set and changed when you click the toggle.
About cookies in javascript:
https://www.w3schools.com/js/js_cookies.asp
Hope this helps!
Related
as a JS noob I'm stuck right now and would highly appreciate some help. My goal is to have a JS pagebrowser that works with content generated by my CMS (TYPO3). Content of different pages is rendered in divs with speaking ids; below you have links to these anchors plus a "show all" link. When you klick on a link to a "page" the respective page content div is shown and the others are hidden; if you click on "show all" all the page content divs are shown. However, if I click a page link after having clicked "show all" not all of the other page content divs are hidden as they should. I guess it has something to do with JS processing order but couldn't find out so far.
window.onload = function () {
var pagelinks = document.querySelectorAll("a.subpage-toggle");
for (var i = 0; i < pagelinks.length; i++) {
pagelinks[i].onclick = function () {
// Finding all elements of class 'active' (creates an array of results)
let x = document.getElementsByClassName("active");
// If class 'active' exists, remove it.
if (x.length > 0) {
x[0].classList.remove("active");
}
if ((this.href.split("#")[1]) !== "show-all") {
// add class 'active' if ID matches href of link
document.querySelector("#" + this.href.split("#")[1]).classList.add("active");
} else {
var subpagecontents = document.getElementsByClassName("subpage-content"),
len = subpagecontents !== null ? subpagecontents.length : 0,
j = 0;
for (j; j < len; j++) {
subpagecontents[j].classList.add("active");
}
}
};
}
};
.subpage-toggle {
display: block;
}
.subpage-content {
display: none;
}
.subpage-content.active {
display: block;
}
<div class="main">
<div id="name-of-page-one" class="subpage-content active">
<p>
Content Page 1
</p>
</div>
<div id="page-two-is-cool" class="subpage-content">
<p>
Content Page 2
</p>
</div>
<div id="nickname-of-page-three" class="subpage-content">
<p>
Content Page 3
</p>
</div>
<div class="pageoverview">
<ul>
<li><a class="subpage-toggle" href="#name-of-page-one">Name of page one</a></li>
<li><a class="subpage-toggle" href="#page-two-is-cool">Page two is cool</a></li>
<li><a class="subpage-toggle" href="#nickname-of-page-three">Nickname of page three</a></li>
</ul>
<a class="subpage-toggle" href="#show-all">Show all</a>
</div>
</div>
JSFiddle: https://jsfiddle.net/Jaydot/62cx5sh0/14/
You need to hide all the previous showing instead of just the first one:
Instead of
x[0].classList.remove("active");
do:
Array.from(x).forEach((element) => element.classList.remove("active"));
https://jsfiddle.net/nvg2aojb/
I know this has been asked, but I cannot figure out how to fix my issue. I have 2 buttons a drink menu and a food menu. I'm able to correctly show the different menus show when their button is clicked. However when the page loads the drink menu appears below the food menu. Once I click food menu buttons the drink menu disappears,and the buttons function as you would expect ie. one menu is shown while the other one is not. I'm not sure what the best method is to hide the drink menu. Any help would be much appreciated.
<button onclick="javascript:toggle('foodMenu')" class="btnMenu">Food</button>
<button onclick="javascript:toggle('drinkMenu')" class="btnMenu">Drink</button>
<div id="foodMenu">
<h1 class="menuHD">Food Menu</h1>
</div>
<div id="drinkMenu">
<h1>Drink Menu</h1>
</div>
var divs = [ "foodMenu", "drinkMenu" ];
function toggle(layer) {
var d
for(var i = 0; i < divs.length; i += 1) {
d = document.getElementById(divs[i]);
d.style.display = 'none';
}
}
I rewrote your script some. I used a class of menu to reference the divs and to set them default to be hidden.
Its also setup to hide any div not selected but only show the selected div.
var divs = [ "foodMenu", "drinkMenu" ];
function toggle(layer) {
var _menus = document.getElementsByClassName("menu");
for(var i = 0; i < _menus.length; i++) {
_menu = _menus[i];
_menu.style.display = 'none';
}
var _menu = document.getElementById(layer);
_menu.style.display = 'block';
}
.menu{
display:none;
}
<button onclick="javascript:toggle('foodMenu')" class="btnMenu">Food</button>
<button onclick="javascript:toggle('drinkMenu')" class="btnMenu">Drink</button>
<div class="menu" id="foodMenu">
<h1 class="menuHD">Food Menu</h1>
</div>
<div class="menu" id="drinkMenu">
<h1>Drink Menu</h1>
</div>
You can toggle by default the food menu, so every time you click a button, you color its text with red, so you know it is selected: its corresponding menu block will show.
Your HTML code:
<button onclick="javascript:toggle('foodMenu')" class="btnMenu" id="foodMenu-btn">Food</button>
<button onclick="javascript:toggle('drinkMenu')" class="btnMenu" id="drinkMenu-btn">Drink</button>
<div id="foodMenu">
<h1 class="menuHD">Food Menu</h1>
</div>
<div id="drinkMenu">
<h1>Drink Menu</h1>
</div>
Your JavaScript code:
// available menu divs
let divs = ["foodMenu", "drinkMenu"];
function toggle(layer) {
// reset all buttons and divs
for (let i = 0; i < divs.length; i += 1) {
let d = document.getElementById(divs[i]);
d.style.display = 'none';
// reset all buttons text colors
let b = document.getElementById(divs[i] + '-btn');
b.style.color = 'black';
}
// set current menu visible
let menu = document.getElementById(layer);
menu.style.display = 'block';
// set current menu button colored "red"
let btn = document.getElementById(layer + '-btn');
btn.style.color = 'red';
}
// select food menu by default
toggle('foodMenu')
I added style="display:none" to the second div in your first block of code. So when the page loads it will display the food Menu.
<button onclick="javascript:toggle('foodMenu')" class="btnMenu">Food</button>
<button onclick="javascript:toggle('drinkMenu')" class="btnMenu">Drink</button>
<div id="foodMenu">
<h1 class="menuHD">Food Menu</h1>
</div>
<div id="drinkMenu" style="display:none">
<h1>Drink Menu</h1>
</div>
For the javascript part use the following code. It will toggle between displaying the food and drink menu.
function toggle(layer) {
const hidingDiv = (layer == 'foodMenu') ? 'drinkMenu' : 'foodMenu';
document.getElementById(layer).style.display = 'block';
document.getElementById(hidingDiv).style.display = 'none';
}
Here is what I have so far, which works fine, but doesn't disappear once another link is activated.
function skillsFunction () {
var x = document.getElementById("mySkills");
if (x.style.display == "none"){
x.style.display = "block"; {}
} else {
x.style.display = "none";
}
}
let openTab = (event, tabNumber) => {
const tabContent = document.getElementsByClassName("tabContent");
for(let tab of tabContent) {
tab.style.display = "none";
}
document.getElementById(tabNumber).style.display = "block";
}
<div class="tabs">
<span class="tab" onclick="openTab(event,1)">Projects</span>
<span class="tab" onclick="openTab(event,2)">Skills</span>
<span class="tab" onclick="openTab(event,3)">Courses</span>
</div>
<div class="mySkills" id="2"></div>
<div class="myCourses" id="3"></div>
<!--START OF PROJECTS WRAPPER-->
<div class="projectsWrapper" id="1">
This is my first time using JavaScript.
Alrighty, first off it is typically not good practice to use HTML onclick="" to handle clicks, so we would rather use JS to attach an event listener.
In the HTML, add the class of tabContent, which was missing, an id to each tab, and add "div" + tabId to each body content (Ids can be whatever you want just keep them the same). Also add display:none to initially start the content closed (probably choose one to start open and leave that without display:none) (And of course change the layout/whatever to fit the rest of your HTML):
Then, in the JS initialize tabs and tabContent, and for each tab add a click event listener which hides every tabContent then displays the one that matches the tabId.
const tabs = document.getElementsByClassName("tab");
const tabContent = document.getElementsByClassName("tabContent");
for(var i = 0; i < tabs.length; i++) {
tabs[i].addEventListener("click", function(event) {
for(var i = 0; i < tabContent.length; i++) {
tabContent[i].style.display = "none";
}
var tabId = event.target.id;
document.getElementById("div" + tabId).style.display = "block";
});
}
<span id="PROJECTS" class="tab">Projects</span>
<span id="SKILLS" class="tab">Skills</span>
<span id="COURSES" class="tab">Courses</span>
<div class="mySkills tabContent" id="divSKILLS" style="display:none">
Skills
</div>
<div class="myCourses tabContent" id="divCOURSES" style="display:none">
Courses
</div>
<div class="projectsWrapper tabContent" id="divPROJECTS" style="display:none">
Projects
</div>
Let me know how it goes!
JsFiddle
I've attempted to code vanilla Javascript that opens and closes buttons (tabs) and shows content.
They show the content correctly, but don't hide the content once clicked.
I've 'reverse engineered' the code that the opens the tab, but this code hides the content and the button when clicked.
Clearly my code is wrong, but i feel that i'm so close to achieving what i set out to achieve. So i'm looking to edit the existing code, not try not change anything drastically.
Cheers
function openTab(click, openTab) {
var i;
var content;
var link;
content = document.getElementsByClassName("content");
for (i = 0; i < content.length; i++) {
content[i].style.display = "none";
}
links = document.getElementsByClassName("link");
for (i = 0; i < links.length; i++) {
links[i].className = links[i].className.replace("active", "");
}
document.getElementById(openTab).style.display = "block";
for (i = 0; i < links.length; i++) {
click.currentTarget.className += "active";
}
document.getElementById(openTab).style.display = "active";
click.currentTarget.style.display = "none";
}
<div class="tabs">
<button class="link" onclick="openTab(event, 'About')">About</button>
<button class="link" onclick="openTab(event, 'Hire')">Why You Should Hire Me</button>
<button class="link" onclick="openTab(event, 'Contact')">Contact</button>
</div>
<div id="About" class="content">
</div>
<div id="Hire" class="content">
</div>
<div id="Contact" class="content">
</div>
The code you posted had some confusing behaviour (such as the buttons disappearing completely). I removed the line that made buttons disappear, as well two different loops that seemed to conflict with each other regarding the class name of the links.
I edited the code down to something simple that displays the content according to the button clicked, but I suspect I've misunderstood something and you're after something else. Maybe you can clarify what's missing?
function openTab(click, openTab) {
var i;
var content;
var wasOpen = document.getElementById(openTab).style.display === "block";
content = document.getElementsByClassName("content");
for (i = 0; i < content.length; i++) {
content[i].style.display = "none";
}
if (wasOpen) return;
document.getElementById(openTab).style.display = "block";
}
<div class="tabs">
<button class="link" onclick="openTab(event, 'About')">About</button>
<button class="link" onclick="openTab(event, 'Hire')">Why You Should Hire Me</button>
<button class="link" onclick="openTab(event, 'Contact')">Contact</button>
</div>
<div id="About" class="content" style="display:none">
ABOUT CONTENT
</div>
<div id="Hire" class="content" style="display:none">
HIRE CONTENT
</div>
<div id="Contact" class="content" style="display:none">
CONTACT CONTENT
</div>
Explainer:
The changes I made to the html was 1- to add some text in each tab and 2- set all tabs to display:none
Within the javascript:
On click, we have a value for "openTab", representing one of the tabs. The line:
var wasOpen = document.getElementById(openTab).style.display === "block";
Sets a Boolean variable which is true if "openTab"'s display property is set to block.
Then we set all tabs to display:none with the following:
content = document.getElementsByClassName("content");
for (i = 0; i < content.length; i++) {
content[i].style.display = "none";
}
And now, depending on whether or not the tab was already open, we either leave the function already, or set the tab to "block"
if (wasOpen) return;
document.getElementById(openTab).style.display = "block";
Tadaaaa!
I'm trying to use accordian to create my page.
It works fine when I click the button but the only problem is that the div comes open on page load and I want it to be closed. It should only open when the button is clicked.
Click here
<div class="panel" id="AccordionDiv">
<div class="store">
<div class="store-row">
<div class="cells store-logo text-center">
<img src="#strStaticWebsiteUrl#(objOfferPrice.StoreImage)" alt="" />
</div>
#if (objOfferPrice.Price < objOfferPrice.UrlPrice)
{
<div class="cells text-center">
<div class="product-price offer-price">Rs. #String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:0,0}", objOfferPrice.Price)<sup>*</sup></div>
<p class="real-price">Price: #objOfferPrice.UrlPrice</p>
</div>
}
</div>
</div>
This is html code and below is the Script code.
<script>
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function () {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
});
}
</script>
Simply add style="display:none" to your pannel div
Change
<div class="panel" id="AccordionDiv">
To
<div class="panel" id="AccordionDiv" style="display:none">
Wokring demo
With the code you have, set an inline style="display:none" so the accordion will be closed by default. The recommended way would be to have classes as open/close and toggle between them rather than having styles inlined.
Hide it initially and show it when button click fires
<div class="panel" id="AccordionDiv" style="display:none";>
<script>
window.onload = function(){
var acc = document.getElementByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function () {
document.getElementById("AccordionDiv").style.display="block";
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
});
}}
</script>