How to Connect HTML to JavaScript?

Estimated read time 1 min read

To connect HTML to JavaScript, you can use the <script> tag in HTML to include JavaScript code.

Here’s an example:

<!DOCTYPE html>
<html>
  <head>
    <title>My Web Page</title>
  </head>
  <body>
    <h1>Welcome to my web page</h1>
    <p>Click the button to change the text:</p>
    <button onclick="changeText()">Click me</button>
    <p id="myText">Hello World!</p>
    
    <script>
      function changeText() {
        document.getElementById("myText").innerHTML = "Hello JavaScript!";
      }
    </script>
  </body>
</html>

In this example, the JavaScript code is included in the <script> tag within the HTML document. The changeText() function changes the text inside the p element with the id of “myText” when the button is clicked.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply