Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Was recently trying to play around JavaScript until I got stuck here...

I made four menu headers, each of which when clicked turns red, to show that the menu header is "active". The problem however is, I want just one to turn red at a time, instead of all menu headers turning red for every header clicked.

What I have tried:

HTML
<nav id="nav_menu_query_off">
        <menu id="main_menu">
            <li class="main_list_item">
                <div class="menu_header">menu one</div>
                <div class="menu_body">
                    <menu class="sub_menu"></menu>
                    <menu class="sub_menu"></menu>
                    <menu class="sub_menu"></menu>
                </div>
            </li>
            <li class="main_list_item">
                <div class="menu_header">menu two</div>
                <div class="menu_body">
                    <menu class="sub_menu"></menu>
                    <menu class="sub_menu"></menu>
                    <menu class="sub_menu"></menu>
                </div>
            </li>
            <li class="main_list_item">
                <div class="menu_header">menu three</div>
                <div class="menu_body">
                    <menu class="sub_menu"></menu>
                    <menu class="sub_menu"></menu>
                    <menu class="sub_menu"></menu>
                </div>
            </li>
            <li class="main_list_item">
                <div class="menu_header">menu four</div>
                <div class="menu_body">
                    <menu class="sub_menu"></menu>
                    <menu class="sub_menu"></menu>
                    <menu class="sub_menu"></menu>
                </div>
            </li>
        </menu>
    </nav>


CSS
*{
	margin: 0;
	padding: 0;
	box-sizing: border-box;
	text-decoration: none;
	list-style: none;
}

body{
	font-family: 'Ebrima';
	background-color: #444444;
}

nav#nav_menu_query_off{
	position: fixed;
	top: 0;
	left: 0;
	width: 200px;
	height: 100vh;
	background-color: #222222;
	overflow: auto;
	z-index: 2;
	padding: 20px 0 20px 20px;
}
nav#nav_menu_query_off menu#main_menu li.main_list_item div.menu_header{
	text-transform: uppercase;
	padding-bottom: 10px;
	cursor: pointer;
}
nav#nav_menu_query_off menu li{
	color: #f0f0f0;
}
nav#nav_menu_query_off menu#main_menu li.main_list_item:not(:first-child){
	padding-top: 20px;
}
.active_red{
	color: red;
}

nav::-webkit-scrollbar{
    display: none;
}


JavaScript
let menuHeader = document.querySelectorAll('div.menu_header');

menuHeader.forEach((head) => {
    head.addEventListener('click', ()=>{
        if (!head.classList.contains('active_red')) {
            head.classList.add('active_red');
        } else {
            head.classList.remove('active_red');
        }
    })
})


Please help me anyone?
Posted
Updated 4-Jun-22 10:02am
v2

1 solution

First turn them all off then turh on the one you clicked

HTML
let menuHeader = document.querySelectorAll('div.menu_header');

menuHeader.forEach((head) => {
    head.addEventListener('click', (e)=>{
		menuHeader.forEach((h) => {
			h.classList.remove('active_red');
		})	
        e.target.classList.add('active_red');
    });
});
 
Share this answer
 
Comments
Skyroxen 7-Jun-22 4:05am    
Wow! This works just fine. Except that the now active header when clicked again doesn't turn back to white. Please help me fix this, pleeeeeeaaaaase?
Thank you so very much.
Mike Hankey 7-Jun-22 10:50am    
It will return to whatever color the menu item was originally set to.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900