Hey everyone,I'm new here, and also quite new to coding. I finished javascript, jquery & html + css courses at codecademy (great website for beginners) and I'm currently making a mock-up website, just for experimentation.
I'm trying to make a menu that can be controlled by the arrow keys but I can't seem to get it right. I want a marker in front of the selected item to move up & down. My menu consists of a few list elements of which I turned off the markers. At the moment the section of my code looks like this:
The HTML:
<div class="menu"> <ul> <li class="selected">Home</li> <li>Work</li> <li>About</li> <li>Contact</li> </ul></div>
The CSS (the display: none is because I let it fade in in jquery, but this isn't relevant for my question):
.menu ul { font-family: Julius Sans One; font-weight:bold; display: none; margin-top:35px; margin-left: 0px; position: relative; list-style-type: none; }li { margin-top:20px; } .selected { list-style-type: square;}
The jQuery:
var main = function(){ $(document).keydown(function(event) { if (event.keyCode === 40){ $('.selected').removeClass('selected'); $('.selected').next().addClass('selected'); } else if (event.keyCode === 38){ $('.selected').removeClass('selected'); $('.selected').prev().addClass('selected'); } }); };$(document).ready(main);
So basically, I want to add the class 'selected' to the selected element and remove it from the previous. Like this, my marker just disappears when I press my arrow down or arrow up key. If I remove the first line in the if and else if statement, it adds the markers but doesn't remove the ones on the previous line, so if i pres all the way down to the end of the menu, there are markers in front of all the menu items.
I tried to google it, but my coding brain isn't trained that well yet and I couldn't really find cases that were similar enough to apply the methods to my code. If someone more experienced could help me out with this one, that would be great.