How to Use HTML and JavaScript Together?

Estimated read time 1 min read

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.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply