Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

toggleplay button javascript

/* make button element in html (youn can find the JavaScript at the 
end of this guide, the purpose of this guide is to guide beginners like me
who are confused on what to do.)

        <button id="btnpp" onclick="playPause()" type="button"></button>

edit with css and set the default background image into pause.png
    #btnpp {
    padding: 30px 30px;
    background-image: url('../img/btnTexture/play.png');
    background-size: cover;
    border-radius: 180px;
    border: none;
    cursor: pointer;
    }

    #btnpp:hover {
    background-size: cover;
    opacity: 0.5;
    } 
    
    I've failed a lot of time figuring out the perfect toggle play/pause button..
    I'd done a lot of research but nothing helpful at the end..
    So here it is.. I made it using the basic Logic of constructing a javascript
    toggleplay button.
    */

//apply button onclick function
function playPause() {
  //add var to the id "btnpp" button for the second function onclick..
    var btn = document.getElementById("btnpp"); //edit to your own button id
  //add var to the id "of your song/video"
    var ricksound = document.getElementById("rickRoll");
    if (ricksound.play) {
        ricksound.pause();
      
      //change button image to play.img when paused
        btn.style.backgroundImage = "url('img/btnTexture/play.png')" //edit to your own path
        
      //put another onclick function same id "btnpp" with var name 'btn'
        btn.onclick = function() {
            if (ricksound.paused) {
                ricksound.play();
              
              //change the button image to pause.img when played
                btn.style.backgroundImage = "url('img/btnTexture/pause.png')"
            } else {
                ricksound.pause();
              
              //change button image to play.img when paused
                btn.style.backgroundImage = "url('img/btnTexture/play.png')"
            }
        }
    }
}

/* 
So you see.. instead of using...
    function playSound() {
    var ricksound = document.getElementById("rickRoll");
    if (ricksound.play) {
        ricksound.pause();
        } else {
        ricksound.play();
        }
    }
    
I'm telling you, it's not gonna work! that's why I made another
button onclick function inside of the button playSound function
in order to repeat the function just like a "Recycle, Reuse, Reduce"
cycle diagram.. so that the two function will kept repeating when
button is pressed.. 

    Function playSound "pause" --> Onclick Function "play" --> Function playSound "pause"
    |<----------------------------------------------------------------------------------|
    
if it didn't work for you then feel free to message me or comment and I'll reply..
*/
Comment

PREVIOUS NEXT
Code Example
Javascript :: Minimum Path Sum for loop 
Javascript :: multi command run in one in terminal npm 
Javascript :: js create an object from another object with some keys removed 
Javascript :: javascript custom table 
Javascript :: Is It Negative Zero (-0)? js 
Javascript :: js notimplemented error 
Javascript :: save for wanver dev 
Javascript :: react leaflet layer disable controls while on top 
Javascript :: javascript interview questions geeksforgeeks 
Javascript :: jquery code convert into javascript online 
Javascript :: how to multiply two array in javascript 
Javascript :: Fetch data from multiple pages of an API in react native 
Javascript :: forming a magic sqare hackerank in javascript 
Javascript :: how to get specific property name with numbers from object in javascript 
Javascript :: Ajax in wordpredss 
Javascript :: javascript spread operator works on what structure 
Javascript :: how to check my javascript code 
Javascript :: react three fiber set cursor pointer 
Javascript :: js get data from liocalstorage 
Javascript :: js how to shuffle array algoritm. The Fisher-Yates algorith 
Javascript :: laravel data showing in single option instead of multiple option from json array 
Javascript :: javascript vuelidate identical passwords only if checkbox is ticked 
Javascript :: Changing the value of a dropdown when the value of a number box changes in AngularJS 
Javascript :: Prevent the wiping of an Array after routing Angular.js 
Javascript :: how to set a condition so that between the first and second mouse clicks there was a delay not 500mls 
Javascript :: context Menus 
Javascript :: sort lowest to highest js 
Javascript :: parse json keep the order 
Javascript :: mustache tutorial javascript 
Javascript :: ... Notation In JavaScript 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =