methods in java script and calling multiple methods

why can't i call both changeBackground() and toNormal() as method from custom

when i remove toNormal as a method and place it outside custom as a stand-alone function the code works but while in custom it does not

can an object hold more than one method

var custom = { changeBackground : function(){ var text = document.getElementById("para").style.backgroundColor ="red"; } toNormal : function(){ var text = document.getElementById("para").style.backgroundColor =""; } }


 this is some text 

1 answer

  • answered 2018-11-08 07:58 kemicofa

    You forgot the , after your changeBackground function. A list of objects requires them to be seperated by a comma.

    var custom = {
      changeBackground: function() {
        var text = document.getElementById("para").style.backgroundColor = "red";
      }, //<-- this
      toNormal: function() {
        var text = document.getElementById("para").style.backgroundColor = "";
      }
    }
    
    custom.changeBackground();
    
    setTimeout(custom.toNormal, 1000);
    div {
      width: 100px;
      height: 100px;
    }
    <div id="para"></div>