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>
    <title>My Web Page</title>
    <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>
      function changeText() {
        document.getElementById("myText").innerHTML = "Hello JavaScript!";

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