I cannot get my alternate background-image to be toggled to when I click on my toggle checkbox 'button'. I inputted my relevant code into CodePen and it was able to toggle correctly; however, when I copy it into my text editor (as shown in the code below, it fails to even get to the alert statement nested within this line: "$('input[type="checkbox"]').click(function(){".
I followed "https://www.w3schools.com/howto/howto_css_switch.asp" to create the switch; however, unclear as to why they nested input into the label tag, or why they included a span tag. Any help or explanation would be greatly appreciated.
HTML (part of my code):
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
CSS (part of my code):
body {
background-image: url(background.png);
...
}
/* the box around the slider */
.switch {
position: absolute;
display: inline-block;
width: 60px;
height: 34px;
top: 37px;
left: 40px;
}
/* hide default HTML checkbox */
.switch input {
opacity: 0;
width: 0;
height: 0;
}
JAVASCRIPT (all of my code):
$('input[type="checkbox"]').click(function(){
alert("got here");
if($(this).is(":checked")){
$('body').css("background-image", "url(background-dark.png)");
}
else if($(this).is(":not(:checked)")){
$('body').css("background-image", "url(background1.png)");
}
});
I expected the background image to change to 'background-dark.png' every time I clicked the toggle switch; however, nothing seems to happen when I repeatedly click on the toggle switch.