Hi,
How to generate random number in HTML each time user click on a button.
Example: 9056
6547
1432
Hi makenzi.exc,
Refer below sample.
HTML
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style type="text/css"> body { font-family: Arial; font-size: 10pt; } table { border: 1px solid #ccc; } table th { background-color: #F7F7F7; color: #333; font-weight: bold; } table th, table td { padding: 5px; border-color: #ccc; } </style> </head> <body> <input type="button" onclick="GenerateRandomNumber()" value="Generate Random Number" /> <br /> <span id="lblMessage"></span> <script type="text/javascript"> function GenerateRandomNumber() { var minVal = 1000; var maxVal = 9999; var randNum = Math.floor(Math.random() * (maxVal - minVal + 1)) + minVal; document.getElementById("lblMessage").innerHTML = randNum; } </script> </body> </html>
Demo
© COPYRIGHT 2025 ASPSnippets.com ALL RIGHTS RESERVED.