Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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