HTMLStarter.com
HTML, CSS, JavaScript for beginners - Interactive Training

Introduction to DHTML

Dynamic HTML or DHTML is just a method of modifying style properties of an element by Javascript in response to a user action for the purpose of making a web page or a part of it dynamic and interactive. The static part of a page such as this paragraph is meant only for reading and does not respond to clicks or mouse overs, while the dynamic parts like the examples ahead respond to mouse actions.

Making an element dynamic can measn changing its colors, moving it from one position to another or along a continuous path, changing its content, making it visible or invisible, poping it up as message block. In order to do all this the lement has to have an id attribute with a unique name. Remember this attribute in Core Attributes?

On this page...

Changing the Color and Other Properties of an Element

As an example, you can change the background color of the menu on the left by clicking on the boxes below.

                 

The code is amazingly simple. The above rectangles are nothing else but cells of a table with different bgcolor values, and an onclick attribute. Here is code for the yellow cell.

<td bgcolor="yellow" onclick = "document.getElementById('menu').style.backgroundColor = 'yellow'"> </td>

The getElementById('menu') seeks the element whose id is 'menu', in fact our menu is contained in a DIV with id="menu". Then accesses its style and assigns a value of 'yellow' to the background color. All spellings must be exactly as written here, if you write 'backgroundcolor', with a small 'c', it doesn't work

Another example is to change the text properties when the mouse cursor is placed over it. Place your mouse over the following text.

mouse over

Several things happen to this text and the best way to explain it is to see the code. Note that two codes are required, one for mouseover and the other for mouseout. The later are siply empty strings so that default values will apply. Another point to observe is that instead of getElementById we used the word this. This is used when the action to be taken is on the element for which the Javascript code is written.

<span onmouseover="this.style.color='red'; this.style.cursor='wait'; this.style.fontSize='20px'; this.style.textDecoration='overline'" onmouseout = "this.style.color=''; this.style.cursor=''; this.style.fontSize=''; this.style.textDecoration=''" >mouse over</span>

TOP

Hiding and Displaying an Element

Another simple action is to remove the menu alltogether. Click on the following buttons.

The codes for these buttons are:

<button onclick="document.getElementById('menu').style.visibility='hidden'">Hide menu</button>
and
<button onclick="document.getElementById('menu').style.visibility='visible'">Show menu</button>

The key word here is the style property visibility. If it is assigned a value 'hidden', the element dissapears, the value 'visible' displays it.

TOP

Changing the Position of an Element

Is the menu of this page too high uo? You can pull it down here. Click on the following buttons.

The codes for these buttons are:

<document.getElementById('menu').style.position='relative'; document.getElementById('menu').style.top='1000px'" >Reposition menu</button>
and
<document.getElementById('menu').style.top='0px'">Take it back</button>

In this case we are playing with the element positioning style properties. The position property can be either absolute or relative. The absolute setting measure distances from the top left corner of the container, while the relative setting measures it from where it is now. Once this is set, we employ style.top to move it up or down, and style.left to move it right or left.

TOP

Changing the Content of an Element

In the previous section we saw that the content of a form element can be changed by accessing the element's content thrrough its HTML structure like document.form1.Perc.value. This can only be done to form elements, not to other elements such as our menu. However, there is a way to change the content of any element as long as it has an id assigned to it. And it is by
getElementById("id-of-element").innerHTML = new-content.

The following example changes the content of the lower line when the mouse is placed on the four tabs

HTML Styles Javascript DHTML
.

Here is the code. It looks complex, but the principle is simple and has to be repeated for the four tabs.

<table cellspacing="0" cellpadding="6" width="600" bgcolor="#b8c7c3" style="border:1px solid black">
<tr><td bgcolor="#d3e3f2" onmouseout="this.style.backgroundColor=''; document.getElementById('panl').innerHTML='&nbsp;'" onmouseover="this.style.backgroundColor='#99ccff'; document.getElementById('panl').innerHTML= '<b>HTML</b> constructs tha basic web page'">HTML</td>
<td width="25%" bgcolor="#d3e3f2" onmouseout="this.style.backgroundColor=''; document.getElementById('panl').innerHTML='&nbsp;'" onmouseover="this.style.backgroundColor='#99ccff'; document.getElementById('panl').innerHTML= '<b>Styles</b> render various element features, mostly programmable'">Styles</td>
<td width="25%" bgcolor="#d3e3f2" onmouseout="this.style.backgroundColor=''; document.getElementById('panl').innerHTML='&nbsp;'" onmouseover="this.style.backgroundColor='#99ccff'; document.getElementById('panl').innerHTML= '<b>Javascript</b> is a programming language running in the browser'">Javascript</td>
<td width="25%" bgcolor="#d3e3f2" onmouseout="this.style.backgroundColor=''; document.getElementById('panl').innerHTML='&nbsp;'" onmouseover="this.style.backgroundColor='#99ccff'; document.getElementById('panl').innerHTML= '<b>DHTML</b> combines styles and Javascript to make pages dynamic and interactive'">DHTML</td>
<tr><td width="100%" colspan="4" bgcolor="#99ccff" id="panl">.</td></tr>
</tr>
</table>

To complete the essential technical parts of a web page design, we will see how to layout the web page in the next section.

TOP

 

Send This SITE To A Friend   |  Add to favorites   |  Terms of service   |  Privacy statement   |  Contact us
Copyright © 2009-2012 HTMLStarter.com