NAFarr
NAFarr

Reputation: 67

Video mute/unmute button javaScript

JavaScript beginner here!

I am trying to make a video player in javaScript for a school project but I am having trouble with my mute button.

I want the button to mute the video when clicked and unmute if the button is pressed again. So far I have only been able to mute the video and keep it muted.

Here is my current mute button.

var button = document.getElementById('mute');
    button.onclick = function (){
        video.muted = true;
    };

I tried an if else statement but it was unsuccessful

var button = document.getElementById('mute');
    button.onclick = function (){

    if (video.muted = false) {    
           video.muted = true;
    }

    else {
        video.muted = false;
    }

    };

Thanks for your help.

Upvotes: 3

Views: 28093

Answers (3)

Jade
Jade

Reputation: 90

The reason is because instead of you are actually checking whether muted attribute is false or true, you are assigning false to the muted attribute.

So you should write as follow:

if (video.muted === false) {    
    video.muted = true;
}
else {
    // your code
};

Upvotes: 1

Qwerty
Qwerty

Reputation: 32048

Enter Bitwise XOR (^)

video.muted ^= 1

Upvotes: 3

David Li
David Li

Reputation: 1290

if (video.muted = false) {    
       video.muted = true;
}

This should be

if (video.muted === false) {    
       video.muted = true;
}

Or else the else statement will never run, since you are setting video.muted to false every time in the if statement itself.

Upvotes: 11

Related Questions