0% found this document useful (0 votes)
10 views

JavaScript and HTML

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

JavaScript and HTML

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

HTML Section

The HTML part of your code sets up the structure of the webpage:

 <!DOCTYPE html>: This declaration defines the document as an HTML5 document.

 <html>: This tag is the root element of an HTML page.

 <body>: This element contains the content of the HTML document.

Inside the body, you have a heading and a paragraph:

 <h2>Use JavaScript to Change Text</h2>: This is a level 2 heading.

 <p>This example writes "Hello JavaScript!" into an HTML element with id="demo":</p>: This paragraph
describes what the JavaScript code will do.

 <p id="demo"></p>: This paragraph element has an id attribute with the value "demo". This is where the
JavaScript will insert the text.

JavaScript Section

The JavaScript code is where the magic happens:

 <script>: This tag contains JavaScript code.

 document.getElementById("demo").innerHTML = "Hello JavaScript!";: This line of JavaScript finds the


HTML element with the id "demo" and changes its inner HTML to "Hello JavaScript!".

How It Works

1. When the HTML page loads, the JavaScript code executes.

2. The JavaScript code finds the <p> element with the id "demo".

3. It then sets the content of that element (innerHTML) to "Hello JavaScript!".


So, when you open this HTML file in a web browser, you'll see "Hello JavaScript!" displayed where the <p
id="demo"></p> tag is located. Essentially, JavaScript is dynamically altering the content of the HTML document
after it has loaded. Fun, right?

You might also like