On button press, I want to change the color and background color of a button based on whether or not is "checked" which I'm currently checking by reading the current color.
Currently, it doesn't seem like the CSS value that is being read by jQuery is being updated by the function that jQuery uses to update the CSS.
Here's a JSFiddle that demonstrates the issue.
function toggleButton(button) {
let css = {
"checked": {
"background-color": "#609",
"color": "#fff"
},
"unchecked": {
"background-color": "#ccc",
"color": "#222"
}
}
button.css("color", "#222") ?
button.css(css.checked) :
button.css(css.unchecked)
}
button {
background-color: #ccc;
color: #222;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="toggleButton($(this))">Click me</button>
button.css("color", "#222")does not magically check the currentcolorvalue against"#222"and returnstrue/false:.css()