0

I want to generate random number in this input box by clicking the button.. What is the problem here??

 function myFunction() {
    var x = document.getElementById("demo");
    x.innerHTML = Math.floor((Math.random() * 100) + 1);
 }
<button onclick="myFunction()">Try it</button>
<input type="text" name="demo" id="demo" value="" size="13" disabled="disabled" />
Akhil Aravind
  • 5,741
  • 16
  • 35
mayukhsroy
  • 141
  • 1
  • 1
  • 9

2 Answers2

7

You need to use x.value as this is a input element and it has value property and not innerHTML property.

function myFunction() {
  var x = document.getElementById("demo");
  x.value = Math.floor((Math.random() * 100) + 1);
}
<button onclick="myFunction()">Try it</button>
<input type="text" name="demo" id="demo" value="" size="13" disabled="disabled" />
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

You have to use value instead of innerHTML

x.value= Math.floor((Math.random() * 100) + 1);

piedpiper
  • 520
  • 5
  • 18