简体   繁体   中英

How to make the button clickable more than one time

I don't really know how to name this problem.

I made a simple code where clicking a button results in displaying a random number on screen. But I've run into an issue that every time you display the number then the button "locks" itself and you need to reload the page to click it again. My problem is I don't want to reload everytime I click the button.

<div class="box" id="box">
        <p id="p1">Generate a number</p>
        <p id="p2">  <script src="main.js"></script></p>
        <div class="container">
        <button id="genbutton" class="button"  onclick="generator()">Generate</button>
        </div>
    </div>
function generator() {
    document.getElementById("p2").innerHTML = x;
    document.write.innerHTML(x);

  }

codepen demo

This is because you're only generating x (the random number) once. In your codepen example, move the generation of x into the event handler:

function generator() {
    var x = Math.floor(Math.random() * 1001);
    document.getElementById("p2").innerHTML = x;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM