DHTML Examples
DHTML Examples
DHTML Examples
Visibility
<html>
<body>
<p id="p1">This is a text. This is a text. This is a text. This is a text. This is a text. This is a text. This is a text.</p>
<input type="button" value="Hide text" onclick="document.getElementById('p1').style.visibility='hidden'" />
<input type="button" value="Show text" onclick="document.getElementById('p1').style.visibility='visible'" />
</body>
</html>
Change background colour
<html>
<head>
<script type="text/javascript">
function bgChange(bg)
{
document.body.style.background=bg;
}
</script>
</head>
<body>
<b>Mouse over the squares and the background color will change!</b>
<table width="300" height="100">
<tr>
<td onmouseover="bgChange('red')"
onmouseout="bgChange('transparent')"
bgcolor="red">
</td>
<td onmouseover="bgChange('blue')"
onmouseout="bgChange('transparent')"
bgcolor="blue">
</td>
<td onmouseover="bgChange('green')"
onmouseout="bgChange('transparent')"
bgcolor="green">
</td>
</tr>
</table>
</body>
</html>
EVENTS
Onload
<html>
<head>
<script type="text/javascript">
function mymessage()
{
alert("This message was triggered from the onload event");
}
</script>
</head>
<body onload="mymessage()">
</body>
</html>
Onunload
<html>
<head>
<script type="text/javascript">
function mymessage()
{
alert("This message was triggered from the onunload event");
}
</script>
</head>
<body onunload="mymessage()">
<p>An alert box will display a message when you close this document!</p>
</body>
</html>
Onchange
<html>
<head>
<script type="text/javascript">
function preferedBrowser()
{
prefer=document.forms[0].browsers.value;
alert("You prefer browsing internet with " + prefer);
}
</script>
</head>
<body>
<form>
Choose which browser you prefer:
<select id="browsers" onchange="preferedBrowser()">
<option value="Internet Explorer">Internet Explorer</option>
<option value="Firefox">Firefox</option>
</select>
</form>
</body>
</html>
Onsubmit
<html>
<head>
<script type="text/javascript">
function confirmInput()
{
fname=document.forms[0].fname.value;
alert("Hello " + fname + "! You will now be redirected to www.w3Schools.com");
}
</script>
</head>
<body>
<form onsubmit="confirmInput()" action="http://www.w3schools.com/">
Enter your name: <input id="fname" type="text" size="20">
<input type="submit">
</form>
</body>
</html>
Onreset
<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box was triggered by the onreset event handler");
}
</script>
</head>
<body>
<form onreset="message()">
Enter your name: <input type="text" size="20">
<input type="reset">
</select>
</form>
</body>
</html>
Onselect
<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box was triggered when you selected the content of the input field");
}
</script>
</head>
<body>
<p>Select the content in the input field</p>
<form>
<input type="text" value="Select this text" onselect="message()" size="20">
</form>
</body>
</html>
onblur
<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box was triggered by the onblur event handler");
}
</script>
</head>
<body>
<p>The onblur event occurs when an element loses focus. Try to click or write in the input field below, then click elsewhere in
the document so the input field loses focus.</p>
<form>
Enter your name: <input type="text" onblur="message()" size="20">
</form>
</body>
</html>
Onfocus
<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box was triggered by the onfocus event handler");
}
</script>
</head>
<body>
<form>
Enter your name: <input type="text" onfocus="message()" size="20">
</form>
Onkeydown
<html>
<head>
<script type="text/javascript">
function writeMessage()
{
document.forms[0].mySecondInput.value=document.forms[0].myInput.value;
}
</script>
</head>
<body>
<p>The onkeydown event occurs when the a keyboard key is on it's way DOWN.</p>
<form>
Enter your name:
<input type="text" name="myInput" onkeydown="writeMessage()" size="20">
<input type="text" name="mySecondInput" size="20">
</select>
</form>
</body>
</html>
Onkeyup
<html>
<head>
<script type="text/javascript">
function writeMessage()
{
document.forms[0].mySecondInput.value=document.forms[0].myInput.value;
}
</script>
</head>
<body>
<p>The onkeyup event occurs when the a keyboard key is on it's way UP.</p>
<form>
Write a message:<br />
<input type="text" name="myInput" onkeyup="writeMessage()" size="20">
<input type="text" name="mySecondInput" size="20">
</form>
</body>
</html>
Onkeydown vs onkeyup
<html>
<head>
<script type="text/javascript">
function color(color)
{
document.forms[0].myInput.style.background=color;
}
</script>
</head>
<body>
<form>
Write a message:<br />
<input
type="text"
onkeydown="color('red')"
onkeyup="color('blue')"
name="myInput" size="20">
</form>
</body>
</html>
Onkeypress
<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box was triggered when you pressed a button on your keyboard");
}
</script>
</head>
<body onkeypress="message()">
<p>The onkeypress event is triggered when the user presses a button on the keyboard.</p>
<p>To trigger the onkeypress event, make sure that this window has focus.</p>
</body>
</html>
Onmouseover vs onmouseout
<html>
<body>
<h1 onmouseover="style.color='red'"
onmouseout="style.color='black'">
Mouse over this text</h1>
</body>
</html>
Onclick
<html>
<head>
<script type="text/javascript">
cc=0;
function changeimage()
{
if (cc==0)
{
cc=1;
document.getElementById('myimage').src="bulbon.gif";
}
else
{
cc=0;
document.getElementById('myimage').src="bulboff.gif";
}
}
</script>
</head>
<body>
<script type="text/javascript">
function nameon()
{
document.getElementById('h2text').innerHTML="WELCOME!";
}
function nameout()
{
document.getElementById('h2text').innerHTML="How are you today?";
}
</script>
</head>
<body>
<h2 id="h2text" onmouseout="nameout()"
onmouseover="nameon()">
Mouse over this text!</h2>
</body>
</html>
Change position
<html>
<head>
<script type="text/javascript">
function moveleft()
{
document.getElementById('header').style.position="absolute";
document.getElementById('header').style.left="0";
}
function moveback()
{
document.getElementById('header').style.position="relative";
}
</script>
</head>
<body>
<h1 id="header"
onmouseover="moveleft()"
onmouseout="moveback()">
Mouse over this text</h1>
</body>
</html>
Onmousemove
<html>
<head>
<script type="text/javascript">
var i=1;
function moveright()
{
document.getElementById('header').style.position="relative";
document.getElementById('header').style.left=i;
i++;
}
</script>
</head>
<body onmousemove="moveright()">
<h1 id="header">
Move the mouse over this page
</h1>
</body>
</html>
Onload & onunload
<html>
<head>
<script type="text/javascript">
var i=1
function starttimer()
{
document.getElementById('h_one').style.position="relative";
document.getElementById('h_one').style.left=+i;
document.getElementById('h_two').style.position="relative";
document.getElementById('h_two').style.top=+i;
i++;
timer=setTimeout("starttimer()",10);
}
function stoptimer()
{
clearTimeout(timer);
}
</script>
</head>
<body onload="starttimer()" onunload="stoptimer()">