source: w3schools

DHTML Introduction

previous next

DHTML is the art of combining HTML, JavaScript, DOM, and CSS.


What you should already know

Before you continue you should have a basic understanding of the following:

If you want to study these subjects first, find the tutorials on our Home Page.


DHTML is NOT a Language

DHTML stands for Dynamic HTML.

DHTML is NOT a language or a web standard.

DHTML is a TERM used to describe the technologies used to make web pages dynamic and interactive.

To most people DHTML means the combination of HTML, JavaScript, DOM, and CSS.

According to the World Wide Web Consortium (W3C):
"Dynamic HTML is a term used by some vendors to describe the combination of HTML, style sheets and scripts that allows documents to be animated."


DHTML Technologies

Below is a listing of DHTML technologies.


HTML 4

The W3C HTML 4 standard has rich support for dynamic content:

DHTML is about using these features to create dynamic and interactive web pages.


JavaScript

JavaScript is the scripting standard for HTML.

DHTML is about using JavaScript to control, access and manipulate HTML elements.

You can read more about this in the next chapter of this tutorial.


HTML DOM

The HTML DOM is the W3C standard Document Object Model for HTML.

The HTML DOM defines a standard set of objects for HTML, and a standard way to access and manipulate them.

DHTML is about using the DOM to access and manipulate HTML elements.

You can read more about this in a later chapter of this tutorial.


HTML Events

The W3C HTML Event Model is a part of the HTML DOM.

It defines a standard way to handle HTML events.

DHTML is about creating web pages that reacts to (user)events.

You can read more about this in a later chapter of this tutorial.


CSS

CSS is the W3C standard style and layout model for HTML.

CSS allows web developers to control the style and layout of web pages.

HTML 4 allows dynamic changes to CSS.

DHTML is about using JavaScript and DOM to change the style and positioning of HTML elements.

You can read more about this in a later chapter of this tutorial.

DHTML - JavaScript

previous next

JavaScript can create dynamic HTML content.

Date: document.write(Date()); Fri Jun 05 2009 17:55:37 GMT-0700 (Pacific Daylight Time)


JavaScript Alone

If you have studied JavaScript, you know that the statement:

document.write()

is used to write output to a web page.

Example

Using JavaScript to display the current date:

Example

<html>
<body>

<script type="text/javascript">
document.write(Date());
</script>

</body>
</html>

Try it yourself


JavaScript and the HTML DOM

A JavaScript can also be used to change the content and attributes of HTML elements dynamically.

To change the content of an HTML element:

document.getElementById(id).innerHTML=new HTML

To change the attribute of an HTML element:

document.getElementById(id).attribute=new value

You will learn more about JavaScript and the HTML DOM in the next chapter of this tutorial.


JavaScript and HTML Events

A JavaScript can also be executed when an event occurs, like when a user clicks on an HTML element.

To execute code when a user clicks on an element, use the following event attribute:

onclick=JavaScript

You will learn more about JavaScript and HTML Events in a later chapter.


JavaScript and CSS

JavaScript can also change the style of HTML elements.

To change the style of an HTML element:

document.getElementById(id).style.property=new style

You will learn more about JavaScript and CSS in a later chapter of this tutorial.


previous next

DHTML - HTML DOM

previous next

What is the HTML DOM?

The HTML DOM is:

The HTML DOM defines the objects and properties of all HTML elements, and the methods (interface) to access them.

In other words: The HTML DOM is a standard for how to get, change, add, or delete HTML elements.


Change an HTML Element

The following example changes the content of an HTML h1 element:

Example

<html>
<body>

<h1 id="header">Old Header</h1>

<script type="text/javascript">
document.getElementById("header").innerHTML="New Header";
</script>

</body>
</html>

Try it yourself

Example explained:


Change an HTML Attribute

The following example changes the src attibute of an HTML img element:

Example

<html>
<body>

<img id="image" src="smiley.gif">

<script type="text/javascript">
document.getElementById("image").src="landscape.jpg";
</script>

</body>
</html>

Try it yourself

Example explained:


HTML DOM Tutorial

To learn more about the HTML DOM, find the complete HTML DOM tutorial on our Home Page.


previous next

DHTML Event Handlers

previous next

HTML events can trigger actions in the browser.


Examples

Try it Yourself - Examples

onclick
How to change an image when a user clicks on it.

onmousedown and onmouseup
Change an image when a user holds down the mouse button.

onload
Display an alert box when the page has finished loading.


Event handlers

An event handler allows you to execute code when an event occurs.

Events are generated by the browser when the user clicks an element, when the page loads, when a form is submitted, etc.

In the following example, the h1 heading changes when a user clicks on it:

Example

<html>
<body>
<h1 onclick="this.innerHTML='Ooops!'">Click on this text</h1>
</body>
</html>

Try it yourself

You can also add a script in the head section of the page and then call the function from the event handler:

Example

<html>
<head>
<script type="text/javascript">
function changetext(id)
{
id.innerHTML="Ooops!";
}
</script>
</head>
<body>
<h1 onclick="changetext(this)">Click on this text</h1>
</body>
</html>

Try it yourself


More About HTML Events

For all HTML events, look at our complete HTML DOM Event Object Reference.


previous next

DHTML - CSS

previous next

JavaScript and the HTML DOM can be used to change the style of any HTML element.


Change Style of Current HTML Element

To change the style of the current HTML element, use the statement:

this.style.property=new style

Example

<html>
<body>

<h1 id="header" onclick="this.style.color='red'">Click Me!</h1>

</body>
</html>

Try it yourself


Change Style of a Specific HTML Element

To change the style of a specific HTML element, use the statement:

document.getElementById(element_id).style.property=new style

Example

<html>
<body>

<h1 id="h1" onclick="document.getElementById('h1').style.color='red'">Click Me!</h1>

</body>
</html>

Try it yourself


Examples

Try it Yourself - Examples

Mouse Events
Change the color of an element when the cursor moves over it.

Visibility
How to make an element invisible. Do you want the element to show or not?


More About the Style Object and CSS

For all style properties, look at our complete HTML DOM Style Object Reference.

To learn more about CSS, find the tutorial on our Home Page.


previous next

DHTML Summary - What's Next?

Previous Next

DHTML is about combining HTML, JavaScript, DOM, and CSS.


DHTML is a Term

In this tutorial you have learned that DHTML is only a term used to describe the different combinations of HTML, JavaScript, DOM, and CSS that can be used to create more dynamic web pages.

More DHTML examples


JavaScript

JavaScript is the standard scripting language for the Internet.

Everyone serious about web development should have a full understanding of JavaScript.

Visit our JavaScript tutorial, and our complete JavaScript reference.


The HTML DOM

HTML 4 supports the HTML Document Object Model (DOM).

The HTML DOM is the official (standard and browser independent) way to access HTML elements. It works in all browsers.

Only by using the HTML DOM you can make interactive web pages that will work in all modern browsers.

If you are serious about web development, study our HTML DOM tutorial, and our complete HTML DOM reference.


Dynamic CSS

There is no such thing as dynamic CSS.

However, with JavaScript and the HTML DOM you can dynamically change the CSS style of any HTML element.


Server Side Scripting

In this tutorial you have seen that web pages can be made dynamic by using scripts on the client (in the browser).

Web pages can also be made more dynamic by using scripts on the server.

With server scripting you can edit, add, or change any web page content. You can respond to data submitted from HTML forms, access data or databases and return the results to a browser, and customize pages for individual users.

At W3Schools you can study the following server side scripting tutorials:

PHP tutorial

ASP tutorial

DotNET tutorial


previous next

DHTML Examples

previous next

CSS

Position:relative
Position:relative
Position:absolute
Visibility
Z-index
Z-index Vice Versa
Cursors
Watermark
Change background color

Events

onload
onunload
onchange
onsubmit
onreset
onselect
onblur
onfocus
onkeydown
onkeyup
onkeydown vs onkeyup
onkeypress
onmouseover & onmouseout
onclick
ondblclick
onmousedown & onmouseup
onmousemove
Disable right-click

Text

Element access
Attribute change
innerHTML access
Change innerHTML
Change position
onmousemove
onload & onunload
Tooltip
Typewrite message
Bigger text
Scrolling text
Blinking header

Input Forms

Identical forms
Identical forms 2
Change background color of an input field
Change text color of an input field
Insert background image to an input field
Select all check-boxes
Change background color of a button
Change text color of a button
Insert background image to a button
Change background color of a drop-down list
Change text color of a drop-down list
Change background color of a textarea
Change text color of a textarea
Insert background image to a textarea

Images

Preload image
Change the size of an image
Change the source of an image
Change the size & the source of an image
Change the position of an image
Change the background image
Moving image
Drag and drop the image
Image viewer
Shaking image
Digital clock

Window

Shake the window

Links

Text transform
Letter spacing
Blinking link
Shaking link
Random Banner

Menus

Drop down navigation (select box)
Top drop down
Always-on-top
Inset borders
Description menu
Description image
Cursor description
Cursor image
Sliding horizontal
Click sliding horizontal

Cursor

Cursor position
Cursor text
Cursor image


previous next

DHTML Tutorial

previous next

DHTML

DHTML is NOT a language. It is a TERM describing the art of making dynamic and interactive web pages.

DHTML combines HTML, JavaScript, DOM, and CSS.

Start learning DHTML now!

DHTML Example

Click to turn on/off the light


Try it yourself

DHTML DOM Reference

At W3Schools you will find a complete DHTML DOM reference of all the DOM objects, along with their properties and methods.

DHTML DOM Reference

DHTML Examples

Learn by 100 examples! With our editor, you can edit the source code, and click on a test button to view the result.


previous next