There are multiple divs in my HTML code with different background images. When a div is clicked, a selector is set to true. For instance, if the "jogging" div is clicked, the background color changes from black to orange and the jogging_selected selector is set to true.
Now, I have added a next button (#button_step3) that should hide this section only if one of the divs is selected.
The issue is that it always triggers the "else part" even when a selector is true, and I can't figure out why.
UPDATE
Upon adding console.log(jogging_selected); to the else part of the if query, it logs false. This confuses me because when I click the div again, the color changes back to black, indicating that the selector is true.
Below are the relevant parts of my JavaScript, CSS, and HTML code:
$(document).ready(function() {
var jogging_selected = false;
var soccer_selected = false;
var tennis_selected = false;
var golf_selected = false;
function setBackgroundAndSelector(childnumber, imagepath_black, imagepath_orange, activity_selector){
if(activity_selector){
$(childnumber).css( "background", imagepath_black)
}
else{
$(".image_activitys:nth-child(1)").css( "background", "url(http://dummyimage.com/128x128/000000/fff.png&text=Jogging)" );
$(".image_activitys:nth-child(2)").css( "background", "url(http://dummyimage.com/128x128/000000/fff.png&text=Soccer)" );
$(".image_activitys:nth-child(3)").css( "background", "url(http://dummyimage.com/128x128/000000/fff.png&text=Tennis)" );
$(".image_activitys:nth-child(4)").css( "background", "url(http://dummyimage.com/128x128/000000/fff.png&text=Golf)" );
jogging_selected = false;
soccer_selected = false;
tennis_selected = false;
golf_selected = false;
for(i=1; i<5; i++){
if('.image_activitys:nth-child('+i+')' == childnumber){
$(childnumber).css( 'background', imagepath_orange );
}
}
activity_selector = true;
}
activity_selector = !activity_selector
}
$(".image_activitys:nth-child(1)").click(function () {
setBackgroundAndSelector(".image_activitys:nth-child(1)", "url(http://dummyimage.com/128x128/000000/fff.png&text=Jogging)", "url(http://dummyimage.com/128x128/ff9900/fff.png&text=Jogging)", jogging_selected);
});
$(".image_activitys:nth-child(2)").click(function () {
setBackgroundAndSelector(".image_activitys:nth-child(2)", "url(http://dummyimage.com/128x128/000000/fff.png&text=Soccer)", "url(http://dummyimage.com/128x128/ff9900/fff.png&text=Soccer)", soccer_selected);
});
$(".image_activitys:nth-child(3)").click(function () {
setBackgroundAndSelector(".image_activitys:nth-child(3)", "url(http://dummyimage.com/128x128/000000/fff.png&text=Tennis)", "url(http://dummyimage.com/128x128/ff9900/fff.png&text=Tennis)", tennis_selected);
});
$(".image_activitys:nth-child(4)").click(function () {
setBackgroundAndSelector(".image_activitys:nth-child(4)", "url(http://dummyimage.com/128x128/000000/fff.png&text=Golf)", "url(http://dummyimage.com/128x128/ff9900/fff.png&text=Golf)", golf_selected);
});
// The problematic function
$("#button_step3").click(function() {
if(jogging_selected || soccer_selected || tennis_selected || golf_selected){
$("#wrapper_fitcalc_content_step2" ).css( "display", "none" );
$("#wrapper_fitcalc_content_step3" ).css( "display", "block" );
}
else{
window.scrollTo(0, 0);
alert("Please select a sport");
console.log(jogging_selected);
}
});
});
.image_activitys{
margin: 5px;
width: 128px;
height: 128px;
}
.image_activitys:nth-child(1){
background:url(http://dummyimage.com/128x128/000000/fff.png&text=Jogging);
}
.image_activitys:nth-child(1):hover{
background:url(http://dummyimage.com/128x128/ff9900/fff.png&text=Jogging) !important;
cursor: pointer;
}
.image_activitys:nth-child(2){
background:url(http://dummyimage.com/128x128/000000/fff.png&text=Soccer);
}
.image_activitys:nth-child(2):hover{
background:url(http://dummyimage.com/128x128/ff9900/fff.png&text=Soccer) !important;
cursor: pointer;
}
.image_activitys:nth-child(3){
background:url(http://dummyimage.com/128x128/000000/fff.png&text=Tennis);
}
.image_activitys:nth-child(3):hover{
background:url(http://dummyimage.com/128x128/ff9900/fff.png&text=Tennis) !important;
cursor: pointer;
}
.image_activitys:nth-child(4){
background:url(http://dummyimage.com/128x128/000000/fff.png&text=Golf);
}
.image_activitys:nth-child(4):hover{
background:url(http://dummyimage.com/128x128/ff9900/fff.png&text=Golf) !important;
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<div id="wrapper_fitcalc_content_step2">
<div class="image_activitys" title="Jogging"></div>
<div class="image_activitys" title="Soccer"></div>
<div class="image_activitys" title="Tennis"></div>
<div class="image_activitys" title="Golf"></div>
<!-- There are several more of this divs which I just deleted for my post -->
<input type="button" value="to Step 3" id="button_step3"/>
</div>
Can anyone help me understand why he always goes to the else part in the $("#button_step3").click(function())?