How to unselct other classes | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 3

How to unselct other classes

https://code.sololearn.com/Wxsf3PWIRPZu/?ref=app In the above code, I tried to make the background colour black of the clicked div and at the same, I want to return the background colour of other divs to green. But in the code, when I clicks a div, it becomes black, but it remains black even after clicking other divs. Please tell me how to do such that other divs will become green again while clicking another div (same class) ? *Edit: For example: If I click Home, it will be black and others remain in that color (green). When I click another div (e.g: skills), it should be black and the previously blackened div (home) should return to green. *I want to do it using onclick event. I can do it using hover effect in CSS, but I want to do that when usee clicks instead of hover

2nd Dec 2020, 3:33 PM
Prasant
Prasant - avatar
13 Respostas
+ 2
I can understand your problem very well. I have solved it for you. Checkout my code below, it's just a modification of your code. And it's the simplest approach. https://code.sololearn.com/WDESpndUWWku/#html
4th Dec 2020, 4:16 AM
alsoamit
alsoamit - avatar
+ 11
šŸ’• Prasant šŸ’• āœ³ļø do you want to add hover effect??
2nd Dec 2020, 4:23 PM
慤慤慤
2nd Dec 2020, 4:32 PM
Prasant
Prasant - avatar
+ 2
Mirielle[ INACTIVE ] Ma'am, I know we can do that with hover effect. But I want to do it using onclick event.
2nd Dec 2020, 4:52 PM
Prasant
Prasant - avatar
+ 2
į•™(ļ¼ Ā°ā–½Ā°ļ¼ )į•— {inactive} But I want to do that automatically For example: If I click Home, it will be black and others remain in that color (green). When I click another div (e.g: skills), it should be black and the previously blackened div (home) should return to green.
2nd Dec 2020, 4:57 PM
Prasant
Prasant - avatar
+ 1
A easiest solution I think will be this code: https://code.sololearn.com/W3Lq8YOGEXhG/?ref=app
2nd Dec 2020, 5:04 PM
JaScript
JaScript - avatar
2nd Dec 2020, 7:16 PM
ODLNT
ODLNT - avatar
+ 1
as I get your question you want to do this. This is the way I found when I creating a select effect on my web site. I just answer this to give an idea that you mean to do. i cant post the code here. i ll put it on my codes look at it
3rd Dec 2020, 4:57 PM
Tharindu Warnapura
Tharindu Warnapura - avatar
3rd Dec 2020, 5:08 PM
Tharindu Warnapura
Tharindu Warnapura - avatar
0
šŸ’• Prasant šŸ’• āœ³ļø you should reset your bg color after setting it to black this.style.background = this.style.background ? "" : "black"; and it will be easier if you use jquery
2nd Dec 2020, 4:35 PM
o.gak
o.gak - avatar
0
I think Mirielle[ INACTIVE ] has exhausted everything that JavaScript gives in her example. This could be a bit too much for a beginner, but it's good to know that such a thing exists.
2nd Dec 2020, 5:39 PM
JaScript
JaScript - avatar
0
If you like šŸ’• Prasant šŸ’• āœ³ļø I added in the above code a new feature, swiching off the background if the same button will be clicked second time.
2nd Dec 2020, 6:39 PM
JaScript
JaScript - avatar
0
šŸ’• Prasant šŸ’• āœ³ļø reset other and set current like this: Array.from(menBtns).forEach(btn => btn.style.background = ""); this.style.background = "black";
2nd Dec 2020, 11:17 PM
o.gak
o.gak - avatar