How to create a random number in jquery

  By    Posted on January-12-2018   321

.Net

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>

 

By      12-Jan-2018 Views  321



You may also read following recent Post

Item Image
What are 3 C
 228
Item Image
what is .net
 462
Submit Post - Article - Ask a Question - Ask an Interview Question - Submit Useful Links