Sunday, 21 August 2016

JavaScript form events

Events occurs when user perform some action. Such as on click of a button, certain piece of code will be executed, which is defined in the onclick function of the button.

HTML form events
Event
Description
onsubmit
Fired on submitting a form
onselect
Fired on selecting an element
oninvalid
Fired when an element is invalid
oninput
Fired when input is provided for an element
onforminput
Fired when input is provided on a form
onformchange
Fired when a form change
onfocus
Fired when a window gets focus
oncontextmenu
Fired when the context menu is used
onchange
Fired when an element is changed
onblur
Fired when a window loses focus
onreset
Fired when the fields of form is reset


Example 1 : onreset event

<!DOCTYPE html>
<html>
<head>
    <title>onreset Event</title>
    <script>
        function onreset_event() {
            alert('onreset event is fired when user clicks the reset button');
        }
    </script>
</head>
<body>
    <h1>onreset Event</h1>
    <form name="event" onreset="onreset_event();">
        Favorite Movie : <input type="text" id="movie" /><br />
        <button onmousedown="onmousedown_event();">Click here</button> &nbsp;&nbsp;
        <input type="reset" value="Reset" />
    </form>
</body>
</html>


Example 2: onsubmit event

<!DOCTYPE html>
<html>
<head>
    <title>onreset Event</title>
    <script>
        function onreset_event() {
            alert('onreset event is fired when user clicks the reset button');
        }
        function onsubmit_event() {
            alert('all details has been submitted');
        }
    </script>
</head>
<body>
    <h1>onsubmit Event</h1>
    <form name="event" onreset="onreset_event();" onsubmit="onsubmit_event();">
        Favorite Movie : <input type="text" id="movie" /><br />
        <input type="submit" value="Submit" />
        <input type="reset" value="Reset" />
    </form>
</body>
</html>



0 comments:

Post a Comment