removing a class in javascript

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");
}
RupertAAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leonidas DosasCommented:
You are already have done.I added a onclick event to execute the function and I had made some changes in your js code:
HTML
<div class="list_menu" id="option1" onclick="addMySelectedState();"><a href=""></a></div>

Open in new window

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

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Banshi lal dangiFull Stack DeveloperCommented:
Please try below one, by just replacing the classname, instead of removing first one and adding the new class.
function addMySelectedState()
{
document.getElementById("option1").className = "list_menu_selected";
}

Open in new window

0
RupertAAuthor Commented:
Hi there, both actually work, I wasn't referring to the classes correctly, which is why it was not working! Thanks.
0
RupertAAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
CSS

From novice to tech pro — start learning today.