We help IT Professionals succeed at work.

removing a class in javascript

250 Views
Last Modified: 2017-03-07
Hi, on the css I have two classes as follows...

.list_menu a {
display: block;
padding: 12px 0px;
background: #e1e1e1;
text-decoration: none;
}

.list_menu a:hover {
background: #d4d4d4;
background-repeat:no-repeat;
width:100%;
}

I want to remove them both and add my own Selected state when you click my div, I am using the following javascript function when you click, but it doesn't work, am I referencing the classes wrongly? Thanks.

function addMySelectedState()
{
var nameElem1 = document.getElementById("option1");
nameElem1.classList.remove("list_menu a");
nameElem1.classList.remove("list_menu a:hover");
nameElem1.classList.add("list_menu_selected");
}
Comment
Watch Question

CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Banshi lal dangiFull Stack Developer
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
Hi there, both actually work, I wasn't referring to the classes correctly, which is why it was not working! Thanks.

Author

Commented:
Thanks!