-2

I recently started a new project, so at the moment it is quite simple code. I think there is probably just and error or something I am unaware of that is causing an issue. To a more experienced programmer, this should be an easy fix. My HTML and JavaScript is

var quotecount = 3;

function show() {
  var rand = Math.floor((Math.random() * quotecount) + 1);

  if (rand == 1) {
    document.getElementById("disp").innerHTML = "Quote 1";
    document.getElementById("quotenum").innerHTML = "This is quote 1 out of " + quotecount;
  }

  if (rand == 2) {
    document.getElementById("disp").innerHTML = "Quote 2";
    document.getElementById("quotenum").innerHTML = "This is quote 2 out of " + quotecount;
  }

  if (rand == 3) {
    document.getElementById("disp").innerHTML = "Quote 3";
    document.getElementById("quotenum").innerHTML = "This is quote 3 out of " + quotecount;
  }
}
button {
  background: #fff;
  border: 1px solid #ccc;
  padding: 5px 13px;
  outline:none;
  cursor:pointer;
}

button:hover {
  background: #282828;
  color: #fff;
}
<!DOCTYPE html>
<html>

<body>
  <button id="quoteshow" onclick="show">Click me to show a quote!</button>

  <p id="disp"></p>
  <p id="quotenum"></p>
</body>

</html>

8
  • What's the problem? Commented Aug 11, 2017 at 2:39
  • @clabe45 It doesn't work. When I click the button, nothing happens. Commented Aug 11, 2017 at 2:42
  • Have you tried opening up the javascript console? Commented Aug 11, 2017 at 2:42
  • 1
    javascript belongs in a <script> tag and you should wait for DOM to load, actually call your function, remove the tickmark ` and read console errors Commented Aug 11, 2017 at 2:43
  • you are not calling function show() Commented Aug 11, 2017 at 2:43

2 Answers 2

1

You need to add parenthesis to show in your onclick call in the element.

<button id="quoteshow" onclick="show()">Click me to show a quote!</button>
Sign up to request clarification or add additional context in comments.

Comments

-1

var quotecount = 3;

function show() {
  var rand = Math.floor((Math.random() * quotecount)) + 1;

    if (rand == 1) {
      document.getElementById("disp").innerHTML = "Quote 1";
      document.getElementById("quotenum").innerHTML = "This is quote 1 out of " + quotecount;
    }

    if (rand == 2) {
      document.getElementById("disp").innerHTML = "Quote 2";
      document.getElementById("quotenum").innerHTML = "This is quote 2 out of " + quotecount;
    }

    if (rand == 3) {
      document.getElementById("disp").innerHTML = "Quote 3";
      document.getElementById("quotenum").innerHTML = "This is quote 3 out of " + quotecount;
    }
}
<p id="disp"></p>
<p id="quotenum"></p>

<button onclick="show()">Click me</button>

Add click button.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.