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 doinnerHTML = elem
– Rajesh Commented Jan 27, 2017 at 6:12 -
1
Simply remove the
.value
ininnerHTML = elem.value
; – prtdomingo Commented Jan 27, 2017 at 6:16 - change elem.value to elem. – DHRUV GUPTA Commented Jan 27, 2017 at 6:40
2 Answers
Reset to default 7Try 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条)