In HTML, JavaScript code is inserted between <script> and </script> tags.
<head>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</head>
<body>
<p id="demo"> A Paragraph. </p>
<button class="btn btn-primary mb-5" type="button" onclick="myFunction()">Try it</button>
</body>
In this example, a JavaScript function is placed in the <body> section of an HTML page.
<body>
<p id="demo"> A Paragraph.</p>
<button class="btn btn-primary mb-5" type="button" onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</body>
Scripts can also be placed in external files.
This example links to "myScript.js".
(myFunction is stored in "myScript.js")
A Paragraph.
<body>
<p id="demo"> A Paragraph.</p>
<button class="btn btn-primary mb-5" type="button" onclick="myFunction()">Try it</button>
<script src="myScript.js"></script>
</body>