How to create a random number in jquery
- .Net
-
496
-
January-12-2018
- by
How to create a random number in jquery?
Problem:
I have a web page, i have button, textbox, when user will click on "button" i want to create a random string and set that value in paragraph.
Solution:
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Create A random Number</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script language="javascript" type="text/javascript">
function randomString()
{
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 8;
var randomstring = '';
for (var i = 0; i < string_length; i++)
{
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum, rnum + 1);
}
return randomstring;
}
</script>
<script>
$(document).ready(function ()
{
CreateNumber();
});
function CreateNumber()
{
$(".ideaBox").text(randomString());
}
</script>
</head>
<body>
<button class="generator" onclick="CreateNumber()">Genrate Random Number</button>
<p class="ideaBox"></p>
</body>
</html>
Share This with your friend by choosing any social account