1

I have a javascript function to change the inner HTML of a button according to a function.

Javascript

function event_click_startpause( event ){
              if( interval === null ){
                  start();
                  event.target.innerText = 'pause';}
              else {
                  pause();
                  event.target.innerText = 'Resume';}
                  }
              function start(){
                 pause();
                 interval = setInterval( count, 1000 );
              function pause() {
                 clearInterval( interval );
                 interval = null; }
                                        }

HTML

<button id="pause" class="pause">Pause</button>

CSS

.pause{
background: #c1580b;
color: #ffb734;
width: 70px;
height: 70px;
line-height: 70px;
display: block;
border-radius: 50%;
text-align: center;
text-decoration:none;
border:2px solid #000;
box-shadow: 0px 0px 0px 3px #c1580b;
font-size: medium;
}

So here how can I change the background color of the button to green when changing inner HTML of the button to "resume"?

1
  • I would create another CSS block for Resume, then just change the elements className attribute in your code. Commented May 2, 2018 at 19:37

4 Answers 4

2

See https://jsfiddle.net/xb0yncj7/1/ -

var interval = null;
var count = function() {};

window.event_click_startpause = function(btn) {
    if (interval === null) {
        start();
        btn.innerText = 'pause';
        btn.style.backgroundColor = "#F00"
    } else {
        pause();
        btn.innerText = 'Resume';
        btn.style.backgroundColor = "#0F0";
    }

    function start() {
        pause();
        interval = setInterval(count, 1000);

    }

    function pause() {
        clearInterval(interval);
        interval = null;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer. but this is not working on jsfiddle
Sorry - I didn't save my updates - see jsfiddle.net/xb0yncj7/1
2

You could change the background color

 event.target.css('background-color','#ff00ff');

Comments

2
let pauseButton = document.getElementById('pause');    
pauseButton.classList.add('green');

and on you style add

.pause.green {
    background: green;
}

or use

let pauseButton = document.getElementById('pause');
pauseButton.style.background = 'green';

1 Comment

Here is a fiddle of the working background color. Let me know if it works for you.
2

Though not clear from your JS above, assuming you have the button store as a DOM element in JS, you can use the style method chained with the property of your choosing:

event.target.style.backgroundColor = 'green'

Note: properties are called via camelCase

1 Comment

It is possible then that your event data is not a DOM element

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.