The class (.is-toggled
) is added when clicked on the button , but the styles for the button when clicked is not getting applied...
.is-toggled {
background-color: lightgray;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
}
.gaming-btn {
background-color: black;
color: white;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
}
<button class="gaming-btn" onclick="
const btn = document.querySelector('.gaming-btn');
if(btn.classList.contains('is-toggled')) {
btn.classList.remove('is-toggled')
} else {
btn.classList.add('is-toggled');
}
">Gaming</button>
The class (.is-toggled
) is added when clicked on the button , but the styles for the button when clicked is not getting applied...
.is-toggled {
background-color: lightgray;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
}
.gaming-btn {
background-color: black;
color: white;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
}
<button class="gaming-btn" onclick="
const btn = document.querySelector('.gaming-btn');
if(btn.classList.contains('is-toggled')) {
btn.classList.remove('is-toggled')
} else {
btn.classList.add('is-toggled');
}
">Gaming</button>
Share
Improve this question
edited Mar 19 at 10:46
Ori Drori
194k32 gold badges238 silver badges229 bronze badges
asked Mar 19 at 10:33
Monish MMonish M
251 bronze badge
3
|
2 Answers
Reset to default 3Because the .is-toggled
class is defined before the .gaming-bth
it can't override the styles. Switch the classes order to fix the problem.
You can use classList.toggle
to toggle a class name.
const btn = document.querySelector('.gaming-btn')
btn.addEventListener('click', () => {
btn.classList.toggle('is-toggled')
});
.gaming-btn {
background-color: black;
color: white;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
}
.is-toggled {
background-color: lightgray;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
}
<button class="gaming-btn">Gaming</button>
As Ori Drori already pointed out, you can use the toggle
function. But I will go with your code and make it work:
function myClick(btn) {
if(btn.classList.contains('is-toggled')) {
btn.classList.remove('is-toggled')
} else {
btn.classList.add('is-toggled');
}
}
.gaming-btn.is-toggled {
background-color: lightgray;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
}
.gaming-btn {
background-color: black;
color: white;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
}
<button class="gaming-btn" onclick="myClick(this)">Gaming</button>
So you may wonder what was changed. First, I moved your custom code out from the onclick
into a function and call it instead of hard-coding it as a string. I'm also passing this
to the function and btn
is a parameter instead of querying for the button you just clicked on.
But, more importantly, you had two CSS rules and they contradicted each-other. The two rules were of the same specificity, that is, both of them were appliable in the presence of a CSS class, albeit a different class. CSS in the case of same-specificity rules has the latter rule override the first. So the solution I applied above (which is not the only possible solution) is to increase the specificity of the first, which I did by adding the other class.
is-toggled
background-color is being overridden bygaming-btn
style. Move theis-toggled
so it's aftergaming-btn
(this is mentioned in the answer, but not highlighted as the cause of the issue). – fdomn-m Commented Mar 19 at 10:52