Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Object-Oriented JavaScript (2015) Introduction to Methods Adding a Method to an Object

Ben Ahlander
Ben Ahlander
7,528 Points

Jquery: using .html()

dice.js should generate a random number between 1-6. Than the click function should add the number to the HTML element. Why is this not working?

var dice = {
  roll: function () {
  var sides = 6;
  var randomNumber = Math.floor(Math.random() * sides) + 1;
  console.log(randomNumber);
    $("button").click(function(){
      $("#placeholder").html(randomNumber);
    });//endButtonClick
  }
}
<html>
<head>
    <title>Dice Simulator 2015</title>
    <link rel="stylesheet" href="style.css">
   <script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>
</head>  
<body>
  <p id="placeholder">

  </p>
  <button id="button">Roll Dice</button>
  <script src="dice.js"></script>
<!--  <script src="ui.js"></script>-->

</body>
</html>

Than I thought maybe it has to do with the object literal. So I rewrote it like this. But this still didnt work.

function diceRoll () {
  var sides = 6;
  var randomNumber = Math.floor(Math.random() * sides) + 1;
  console.log(randomNumber);
    $("button").click(function(){
      $("#placeholder").html(randomNumber);
    });//endButtonClick
  }

1 Answer