how to pass return value fo a function to html div in Javascript - Stack Overflow

I need to pass generated Random number from JS function to the html div, but its not able to pass it on

I need to pass generated Random number from JS function to the html div, but its not able to pass it on,here is my code snippet

<html>
<head>
</head>
<body>
    <button id="order" onclick="getRand()">orderId</button>
    <div id="order_id"></div>

    <script>
        var getRand = function () {
            var elem = Math.floor(Math.random() * 89999 + 10000);
            document.getElementById("order_id").innerHTML = elem.value;
        };
    </script>
</body>
</html>

I need to pass generated Random number from JS function to the html div, but its not able to pass it on,here is my code snippet

<html>
<head>
</head>
<body>
    <button id="order" onclick="getRand()">orderId</button>
    <div id="order_id"></div>

    <script>
        var getRand = function () {
            var elem = Math.floor(Math.random() * 89999 + 10000);
            document.getElementById("order_id").innerHTML = elem.value;
        };
    </script>
</body>
</html>
Share Improve this question edited Jan 27, 2017 at 6:34 Carsten Løvbo Andersen 27k10 gold badges50 silver badges79 bronze badges asked Jan 27, 2017 at 6:09 Prasad_JoshiPrasad_Joshi 6623 gold badges15 silver badges35 bronze badges 4
  • 1 = elem.value should be = elem – Andreas Commented Jan 27, 2017 at 6:11
  • 2 elem is a variable. Just do innerHTML = elem – Rajesh Commented Jan 27, 2017 at 6:12
  • 1 Simply remove the .value in innerHTML = elem.value; – prtdomingo Commented Jan 27, 2017 at 6:16
  • change elem.value to elem. – DHRUV GUPTA Commented Jan 27, 2017 at 6:40
Add a ment  | 

2 Answers 2

Reset to default 7

Try with this: Working example :

 <html>
        <body>
        <head><h1>CRS</h1></head>
        <button id= "order" onclick="getRand()">orderId</button>
        <div id="order_id">ff</div>

         <script>
         var getRand = function() {
         var elem = Math.floor(Math.random()*89999+10000);
         document.getElementById("order_id").innerHTML = elem;
           };
         </script>
       </body>
      </html>

You just change elem.value to elem

Check This Answers it is working good

var getRand = function() {
     var elem = Math.floor(Math.random()*89999+10000);
     document.getElementById("order_id").innerHTML = elem;
       };
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id= "order" onclick="getRand()">orderId</button>
    <div id="order_id">ff</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744825369a4595760.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信