HTML JavaScript

HTML JavaScript is used to make web pages interactive and dynamic. With JavaScript, HTML content, styles, and attributes can be changed in response to user actions.

On this page

HTML JavaScript

JavaScript makes HTML pages more dynamic and interactive.

The script Element

The <script> element is used to define client-side JavaScript.

A script can be written directly inside the element or loaded from an external file using the src attribute.

Using JavaScript in HTML

JavaScript is commonly used to manipulate content, styles, and attributes.

To select an HTML element, JavaScript often uses document.getElementById().

document.getElementById("demo").innerHTML = "Hello JavaScript!";

JavaScript Can Change Content

JavaScript can modify the content of HTML elements.

document.getElementById("demo").innerHTML = "Hello JavaScript!";

JavaScript Can Change Styles

JavaScript can dynamically update CSS styles.

document.getElementById("demo").style.fontSize = "25px"; document.getElementById("demo").style.color = "red"; document.getElementById("demo").style.backgroundColor = "yellow";

JavaScript Can Change Attributes

JavaScript can modify HTML attributes.

document.getElementById("image").src = "picture.gif";

The noscript Element

The <noscript> element defines alternative content for users who have JavaScript disabled.

<script> document.getElementById("demo").innerHTML = "Hello JavaScript!"; </script>  <noscript>Sorry, your browser does not support JavaScript!</noscript>

Chapter Summary

  • JavaScript adds interactivity to HTML pages
  • The <script> element defines JavaScript code
  • JavaScript can change content, styles, and attributes
  • The <noscript> element provides fallback content

HTML Script Tags

Tag Description
<script> Defines a client-side script
<noscript> Defines alternate content when scripts are not supported

HTML JavaScript Examples (8)