Can't get a <li> tag to stay highlighted when it has focus
I have a nav bar in which I'm trying to get the <li> elements to behave in a way that they change colors when it has focus. I can't seem to make that work. It' won't keep the focused color. Here is my CSS below:
Re: Can't get a <li> tag to stay highlighted when it has focus
Yes, I want it to be read when the link is hovered but I want it to stay red until another link is clicked. I'm thinking that has to do with the "focus"?
Re: Can't get a <li> tag to stay highlighted when it has focus
Is this what you want? Note that this will only work like this when you've clicked on the element.. if you click anywhere else on the page it will unfocus. The difference here being adding the tabindex
Re: Can't get a <li> tag to stay highlighted when it has focus
Unfortunately that's not working. If you look at the attached image, you'll see a menu item highlighted red. Everytime a menu item is clicked on, I want it to STAY RED until another menu item is clicked on. Hope that helps!
Re: Can't get a <li> tag to stay highlighted when it has focus
A menu item or anywhere on the screen?
I assume your use case is that you want the current menu item to be selected, so when the page loads, it shows the current page as "active." If you want it to stay red until someone clicks into another page then you either need to do it via javascript or add the class server-side, depending on how your application is built.
Re: Can't get a <li> tag to stay highlighted when it has focus
Ok, I think I may have explained it wrong. When the page initially loads, all the menu items are white. When a menu item is clicked on, I want it to stay red until another menu item is clicked, it's basically just toggling the menu items and whichever one was the most recently clicked, that one needs to stay RED.
Re: Can't get a <li> tag to stay highlighted when it has focus
Yeah I understand that, but when you click a menu item, does the page reload and bring you to another page? If so, CSS will not handle that, you have to add the class via javascript or server-side.
Does your page reload when you click a menu item? Or are you talking sub-menu items right now or something?
If you could post your page markup so I could run it, I could tell you what you have to do.
Re: Can't get a <li> tag to stay highlighted when it has focus
Hi techgnome,
I see what you mean here and it makes sense. However, my javascript isn't real sharp. I'm adding an onclick event to the <a> tag for each <li>. What do I pass in the function call that indicates which <li> I'm using. Once in the function, how should I reference the passed parameter?
Re: Can't get a <li> tag to stay highlighted when it has focus
Ok,
I've created a javascript function that should allow for the class to be added. However, I'm terrible with JS and am having issues with this function. Here it is:
Code:
function addMenuClass(id) {
//id is the actual <li> element id
var x = id;
//Get all <li> elements
var li = document.querySelectorAll("li");
for (var i=0; i<li.length; i++) {
li[i].classList.remove(".current");
}
//This alert will display 6 different <li> tag ID's: a1, a2, a3, a4, a5, a6
alert(x);
//Blows up on this statement (see attached screenshot)
x.classList.add(".current");
}
Re: Can't get a <li> tag to stay highlighted when it has focus
IT looks like you're passing in the id of the LI .... not the LI itself... the id has no class... so x.classList doesn't work... that doesn't return anything. You need to pass in the actual li element, not its id. How are you calling the addMenuClass?
Re: Can't get a <li> tag to stay highlighted when it has focus
Hello....
please try below code
Code:
function addMenuClass(li_id)
{
var x =li_id
var li = document.querySelectorAll("li");
for (var i=0; i<li.length; i++)
{
li[i].classList.remove(".current");
}
alert(x);
x.classList.add(".current");
}