/    Sign up×
Community /Pin to ProfileBookmark

Checkbox Toggle not working

I have been experimenting with the following code.
The first checkbox works as I desire when the label is clicked, the background color changes (toggles)
However the second checkbox does not change background color when clicked.
Other than then class=”blue” setting in the second checkbox <input …> statement, they look the same to me.

Can someone tell me why the 1st works as expected, but the 2nd does not ???

[code]
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″ />
<title> CBox Toggle Test </title>

<style>
.lime { background-color: lime; }
.blue { background-color: #abcdef; }
#cb0, #cb1 { opacity: 0; }

/* following does not seem to work when exposed with other attempts
#cb0 input[type=’checkbox’]:checked { background-color: lime; }
#cb1 input[type=’checkbox’]:checked { background-color: lime; }
/* */
</style>

</head>
<body>
<input type=”checkbox” id=”cb0″><label for=’cb0′> CBox 1 Toggle </label> <p />
<input type=”checkbox” id=”cb1″><label for=’cb1′ class=”blue”> CBox 2 Toggle </label>

<script>
function init() {
const sel = document.querySelectorAll(‘input[type=”checkbox”]’); // alert(sel.length);
for (let i=0; i<sel.length; i++) {
sel[i].addEventListener(‘click’,
function () { let x = sel[i].nextElementSibling; x.classList.toggle(‘lime’); } );
};
} init();

</script>
</body>
</html>

[/code]

to post a comment
CSS

3 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJan 19.2019 — The selectors .blue and .lime have the same specifity or priority. If so, the order is relevant and as .blue is located after .lime it overrides. Just exchange both lines and everything will be fine:
.blue { background-color: #abcdef; }
.lime { background-color: lime; }

The subject of specifity is well explained here:

https://medium.com/@dte/understanding-css-selector-specificity-a02238a02a59

The CSS solution will work too if you change it to this:
input#cb0:checked + label { background-color: lime; }
input#cb1:checked + label { background-color: lime; }

https://css-tricks.com/child-and-sibling-selectors/
Copy linkTweet thisAlerts:
@JMRKERauthorJan 19.2019 — Thank you 'Sempervivum', for both solutions.

I was not aware of any color priorities or selector priorities/specificity

Appreciate the information.
Copy linkTweet thisAlerts:
@rootJan 20.2019 — CSS Styles only overwrite if the sheet is loaded in externally and a duplicate entity exists.

.lime and .blue are separate entities within the document, so unless you load up another CSS sheet that has .line or .blue in these valuse won;t be overwritten.

What matters is the order of the styles

Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied.

Over writing will happen if you use the class="blue lime" in a style class which will be the lime as the winner

If you are writing CSS, you should avoid using any inline styling as this can lead to problems with design.
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...