06 HTML Events
06 HTML Events
Dynamic HTML allows to create really interactive web-pages. It becomes possible using events in
HTML. HTML can process events from keyboard and mouse, and also some specific events, for
example, occurring where form's data was changed. For processing of events data you may use
JavaScript or VBScript.
onabort Occurs if loading of picture is aborted. this event occurs only in <img> tag.
Occurs on error of loading picture (for example, if file not found). this event occurs
onerror
only in <img> tag.
Occurs where a fragment of text was selected. Uses in text <input> elements: text,
onselect
password, textarea.
This simple example shows a basic use of HTML events. If you click on the button, alert message with
text "Click!" will be appears.
<form name="form_test">
<table>
<tr>
<td>Please enter text here:</td>
<td><input type="text" name="entertext"
onkeyup="form_test.readtext.setAttribute('value', form_test.entertext.value.length);"
></td>
</tr>
<tr>
<td>The length of your text is:</td>
<td><input type="text" name="readtext" value="" size="4" readonly></td>
</tr>
</table>
</form>
In this more complex example you may enter some text in top textbox. Quantity of symbols you has
been entered will appear in bottom textbox.
<!DOCTYPE html>
<html>
<body>
<p>When you submit the form, a function is triggered which alerts some text.</p>
function myFunction() {
alert("The form was submitted");
}
</script>
</body>
</html>
Result:When you submit the form, a function is triggered which alerts some text.
Submit
Enter name:
<!DOCTYPE html>
<html>
<body>
<p>A function is triggered when one of the input fields get focus. The function changes the background-
color of the input field.</p>
First name: <input type="text" id="fname" onfocus="myFunction(this.id)"><br>
Last name: <input type="text" id="lname" onfocus="myFunction(this.id)">
<script>
function myFunction(x) {
document.getElementById(x).style.background = "yellow";
}
</script>
</body>
</html>
Result:
A function is triggered when one of the input fields get focus. The function changes
the background-color of the input field.
First name:
Last name: