HTML and JavaScript can be used together in a web page by including JavaScript code within an HTML file using the <script>
tag. The code should be placed within the <head>
or <body>
section of the HTML document, depending on when you want the JavaScript code to be executed.
Here’s an example:
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
</script>
</head>
<body>
<h1>Using HTML and JavaScript Together</h1>
<p id="demo"></p>
<button onclick="myFunction()">Click me</button>
</body>
</html>
In this example, the JavaScript function myFunction
is defined within the <script>
tag in the <head>
section of the HTML document. The function modifies the content of a <p>
element with an id
of “demo” when the “Click me” button is clicked.
+ There are no comments
Add yours