methods in java script and calling multiple methods

1248 views javascript
5

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 


 

answered question

Please format your code properly, you'll see, that a comma is missing.

1 Answer

8

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>

posted this

Have an answer?

JD

Please login first before posting an answer.