What is an Event?
JavaScript's interaction with HTML is handled through events that occur when the user or browser manipulates a page.When the page loads, that is an event. When the user clicks a button, that click, too, is an event. Another example of events is like pressing any key, closing window, resizing window etc.
Developers can use these events to execute JavaScript coded responses, which cause buttons to close windows, messages to be displayed to users, data to be validated, and virtually any other type of response imaginable to occur.
Events are a part of the Document Object Model (DOM) and every HTML element have a certain set of events which can trigger JavaScript Code.
onclick Event Type:
This is the most frequently used event type which occurs when a user clicks mouse left button. You can put your validation, warning etc against this event type.Example:
<HTML>
<head>
<script type="text/JavaScript">
<!--
function sayHello() {
alert("Hello World")
}
//-->
</script>
</head>
<body>
<input type="button" onclick="sayHello()" value="Say Hello" />
</body>
</HTML>
|
onsubmit event type:
Another most important event type is onsubmit. This event occurs when you try to submit a form. So you can put your form validation against this event type.onmouseover and onmouseout:
These two event types will help you to create nice effects with images or even with text as well. The onmouseover event occurs when you bring your mouse over any element and the onmouseout occurs when you take your mouse out from that element.Example:
Following example shows how a division reacts when we bring our mouse in that division:<HTML>
<head>
<script type="text/JavaScript">
<!--
function over() {
alert("Mouse Over");
}
function out() {
alert("Mouse Out");
}
//-->
</script>
</head>
<body>
<div onmouseover="over()" onmouseout="out()">
<h2> This is inside the division </h2>
</div></body></HTML>
|
No comments:
Post a Comment