Introduction To DHTML
Introduction To DHTML
Before you continue you should have a basic understanding of the following:
HTML
JavaScript
CSS
If you want to study these subjects first, find the tutorials on our Home Page.
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.
DHTML Technologies
HTML 4
The W3C HTML 4 standard has rich support for dynamic content:
JavaScript
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
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.
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
JavaScript Alone
If you have studied JavaScript, you already know that the statement:
document.write()
Example
<html>
<body>
<script type="text/javascript">
document.write(Date());
</script>
</body>
</html>
Try it Yourself
With HTML 4, JavaScript can also be used to change the inner content and attributes of HTML elements
dynamically.
document.getElementById(id).innerHTML=new HTML
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
New to HTML 4 is the ability to let HTML events trigger actions in the browser, like starting a JavaScript
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.
With HTML 4, JavaScript can also be used to change the style of HTML elements.
document.getElementById(id).style.property=new style
You will learn more about JavaScript and CSS in a later chapter of this tutorial.
DHTML - HTML Document Object Model (DOM)
The DOM presents HTML as a tree-structure (a node tree), with elements, attributes, and text:
Examples
innerHTML
How to access and change the innerHTML of an element.
Attribute change
How to access an image element and change the "src" attribute.
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.
Using the HTML DOM to Change an HTML Element
The HTML DOM can be used to change the content of an HTML element:
<html>
<body>
<script type="text/javascript">
document.getElementById("header").innerHTML="New Header";
</script>
</body>
</html>
HTML output:
New Header
Example explained:
Try it yourself
The HTML DOM can be used to change the attribute of an HTML element:
<html>
<body>
<script type="text/javascript">
document.getElementById("image").src="landscape.jpg";
</script>
</body>
</html>
HTML output:
Example explained:
Try it yourself
If you want to study more about the HTML DOM, find the complete HTML DOM tutorial on our Home Page.
DHTML Event Handlers
HTML events can trigger actions in the browser, like starting a JavaScript when a user clicks on
an element.
Examples
onclick
Turn on the light! How to change an image when the user clicks it.
onload
Displays an alert box when the page has finished loading.
Event handlers
Events are generated by the browser when the user clicks an element, when the page loads, when a form is
submitted, etc.
Example
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:
<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>
For a full overview please refer to the complete DOM Event Object Reference in our HTML DOM tutorial.
DHTML CSS
Examples
Change the style of the current element
How to change the style of an element using the this object.
With HTML 4, JavaScript and the HTML DOM can be used to change the style any HTML element.
To change the style the current HTML element, use the statement:
style.property=new style
or more correctly:
this.style.property=new style
Try it yourself
document.getElementById(element_id).style.property=new style
Try it yourself
More examples
Mouse Events
How to 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?
To learn more about CSS, find the complete CSS tutorial on our Home Page.
DHTML Summary - What's Next
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.
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 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.
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