Shubh Sheth Shubh Sheth - 4 months ago 17
Javascript Question

know if a boolean changed its value

when I load up the page the console log prints true but when i click the button nothing happens. I think that the event listener might help but i'm not quite good at javascript please help

<button class="shuffle">shuffle</button>
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script type="text/javascript">
var player = $("#audioPlayer")[0];
var length = $("#queue li").length;
var shuffle = true;

$(".shuffle").on('click', function() {
if (shuffle === true) {
shuffle = false;
}else{
shuffle = true;
}
});
if (shuffle === true) {
console.log('true');
}
if (shuffle === false) {
console.log('false');
}

Answer

Define a function that performs what you want to do. Then you can call it when the page loads, and also when the user clicks.

function do_player_stuff(p) {
    if (shuffle) {
        console.log("Shuffling");
    } else {
        console.log("Not shuffling");
    }
}

$(".shuffle").on("click", function() {
    do_player_stuff(player);
    shuffle = !shuffle;
});

do_player_stuff(player);
Comments