Skip to main content

HTML JavaScript

JavaScript makes HTML pages more dynamic and interactive.

Example

A button that shows the date and time when clicked.

Prompt
A page with a heading "My First JavaScript" and a button that, when clicked, shows the current date and time in a paragraph below it.
Output: complete HTML page

The HTML <script> Tag

The <script> tag defines a client-side script (JavaScript). It either holds the script statements directly, or points to an external file with the src attribute. Common uses are image manipulation, form validation, and changing content. To select an element, JavaScript most often uses document.getElementById().

Example

A script that writes text into an element with id="demo".

Prompt
A page with an empty paragraph that has id=demo, and a script that writes "Hello JavaScript!" into it using document.getElementById.
Output: complete HTML page

A Taste of JavaScript

Here are a few things JavaScript can do: change content, change styles, and change attributes of elements.

Change content

Changing an element's content.

Prompt
A page with a paragraph (id=demo) and a button that changes its content to "Hello JavaScript!" using JavaScript.
Output: complete HTML page

Change styles

Changing an element's size, color, and background.

Prompt
A page with a paragraph (id=demo) and a button that changes its font size, color, and background color using JavaScript.
Output: complete HTML page

Change attributes

Changing an image's src attribute.

Prompt
A page with an image (id=image) and a button that changes the image's src attribute to picture.gif using JavaScript.
Output: complete HTML page

The HTML <noscript> Tag

The <noscript> tag defines alternate content shown to users who have scripts turned off, or whose browser does not support scripts.

Example

A script with a <noscript> fallback message.

Prompt
A page with a paragraph (id=demo) set by a script to "Hello JavaScript!", plus a noscript element showing "Sorry, your browser does not support JavaScript!" when scripts are disabled.
Output: complete HTML page