settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Print random numbers in html form with javascript function

+2 votes
How can I print random numbers with JavaScript code/function embeded in HTML form?
asked in JavaScript category by user john7
edited by user golearnweb

1 Answer

+1 vote
 
Best answer

Here's the HTML form with the JS code inside:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<input type="submit" onclick="printRandomNumbers()">
<script>
    function printRandomNumbers() {
        let num = Math.round(
                Math.random() * 100);
        document.body.innerHTML += `<div>${num}</div>`;
    }
</script>
</body>
</html>

 

Note that template literals are used here (line 13)!

Template literals are enclosed by the back-tick (` `) (grave accent) character instead of double or single quotes. Template literals can contain place holders. These are indicated by the Dollar sign and curly braces (${expression}). The expressions in the place holders and the text between them get passed to a function.

 

 

answered by user Jolie Ann
selected by user golearnweb
...