Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 941
NAFarr Avatar asked May 26 '26 08:05

NAFarr


2 Answers

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.

like image 111
David Li Avatar answered May 27 '26 21:05

David Li


Enter Bitwise XOR (^)

video.muted ^= 1
like image 29
Qwerty Avatar answered May 27 '26 22:05

Qwerty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!