ProxyStudent
ProxyStudent

Reputation: 109

JavaScript: Html ID is not responding to Object literal in js

For some reason when I refer to the id "hi"(I know could've had a better name) in js it doesn't seem to recognize it. I think it has to do with the object itself.

var ChristmasHotel = {
  var name: 'ChristmasHotel',
    var rooms: 50,
      getHotelName: function() {
        return this.name;
      }
};


var moreName = document.getElementById('hi');
moreName.textContent = ChristmasHotel.name;
<!DOCTYPE html>
<html>

<body>
  <div>
    <h1>Welcome to <span id='hi'>Null</span></h1>
  </div>
</body>
</html

Upvotes: 0

Views: 36

Answers (1)

Blue
Blue

Reputation: 22911

You have a few errors with your code:

  1. The closing </html> tag was missing a closing >.
  2. In objects you don't put var before when declaring keys.

var ChristmasHotel = {
  name : 'ChristmasHotel',
  rooms : 50,
  getHotelName : function(){
    return this.name;
  }
};


var moreName = document.getElementById('hi');
moreName.textContent = ChristmasHotel.name;

//You can also use:
moreName.textContent = ChristmasHotel.getHotelName();
<!DOCTYPE html>
<html>
<body>
  <div>
    <h1>Welcome to <span id = 'hi'>Null</span></h1>
  </div>
</body>
</html>

Upvotes: 2

Related Questions