Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As per my understanding, you are trying to create '<strong>n' no.of random buttons as of in the <code>innerHTML</code> of <code>#demo</code>.</strong> </p> <p>If so you need to loop it. Try this</p> <pre><code>function myFunction() { var num = 5; document.getElementById("demo").innerHTML = Math.floor(Math.random() * num) var noOfButtons = document.getElementById("demo").innerHTML; for (var i = 0; i &lt; noOfButtons; i++) { var box = document.createElement("BUTTON"); document.body.appendChild(box); } } </code></pre> <p><a href="http://jsfiddle.net/hcgFS/1/" rel="nofollow"><strong>JSfiddle</strong></a></p> <p><em><strong>From your comments:</em></strong></p> <p>1) No display of numbers, so <em>remove the <code>innerHTML</code> of <code>#demo</code> element</em>.</p> <p>2) Reset the button count for each button invoke.</p> <p><em>Whenever you invoke the click event, better reset the value to empty like below</em></p> <pre><code>document.getElementById('demo').innerHTML = ''; </code></pre> <p>3) <em>You need to append the child (<code>box</code>) to the <code>#demo</code> element</em> instead of body like below</p> <pre><code>document.getElementById('demo').appendChild(box); </code></pre> <p>Finally, your code should look like this.</p> <pre><code>function myFunction() { document.getElementById('demo').innerHTML = ''; var num = 5; var noOfButtons = Math.floor(Math.random() * num); console.log(noOfButtons); for (var i = 0; i &lt; noOfButtons; i++) { var box = document.createElement("BUTTON"); document.getElementById('demo').appendChild(box); } } </code></pre> <p><kbd><a href="http://jsfiddle.net/hcgFS/4/" rel="nofollow"><strong>Updated JSFiddle</strong></a></kbd></p> <p>Hope you got some idea.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload