Event listener in JavaScript with example

Ads
 

Event listener in JavaScript with example

What is event listener in JavaScript? Can anyone please post an example of Event listener??

Thanks in Advance!!

View Answers

December 11, 2012 at 9:34 PM

<html>
  <head>
  <title> Add Event Listener Example </title>

</head>
<body onload="init()">
</body>

December 11, 2012 at 9:39 PM

    <html>
     <head>
        <script type="text/javascript">
            function OnButtonDown (button) {
                button.style.color = "#ff0000";
            }
            function OnButtonUp (button) {
                button.style.color = "#000000";
            }

            function Init () {
                var button = document.getElementById
                              ("testButton");
                if (button.addEventListener) {  
                   button.addEventListener ("mousedown",                       
                    function () {OnButtonDown (button)}, false);

                   button.addEventListener ("mouseup", 
                    function () {OnButtonUp (button)}, false);
                }
                else {
                 if (button.attachEvent) {   version 9
                   button.attachEvent ("onmousedown", 
                    function () {OnButtonDown (button)});
                   button.attachEvent ("onmouseup", 
                    function () {OnButtonUp (button)});
                 }
               }
            }
        </script>
     </head>
     <body onload="Init ()">
        Click on the following button and see its text color.
        <br />
        <button id="testButton">Test button</button>
     </body>
    </html>

    Here we are adding two event listeners 
    1.mouseup

December 11, 2012 at 9:39 PM

<html>
 <head>
    <script type="text/javascript">
        function OnButtonDown (button) {
            button.style.color = "#ff0000";
        }
        function OnButtonUp (button) {
            button.style.color = "#000000";
        }

        function Init () {
            var button = document.getElementById
                          ("testButton");
            if (button.addEventListener) {  
               button.addEventListener ("mousedown",                       
                function () {OnButtonDown (button)}, false);

               button.addEventListener ("mouseup", 
                function () {OnButtonUp (button)}, false);
            }
            else {
             if (button.attachEvent) {   version 9
               button.attachEvent ("onmousedown", 
                function () {OnButtonDown (button)});
               button.attachEvent ("onmouseup", 
                function () {OnButtonUp (button)});
             }
           }
        }
    </script>
 </head>
 <body onload="Init ()">
    Click on the following button and see its text color.
    <br />
    <button id="testButton">Test button</button>
 </body>
</html>

Here we are adding two event listeners 
    1.mouseup

September 27, 2011 at 12:49 PM

What is an Event Handling?

An Event - basically, an action or activity that goes on between the user and the application. In Java programming, In Java, Abstract Windowing Toolkit (AWT) handles such class and returns appropriate response to the user.

Event handling Is a method that understands the event and processes it.

Setting an even on button click

<html>

    <script language="JavaScript">
    <!--
    function clickFirstButton()
    {
      document.myForm.button1.click();
    }
    -->
    </script>
    <form name="myForm">
      <input type="button"
             value="Display alert box"
             name="button1"
             onClick="alert('You clicked the first button.')"><br>
      <input type="button"
             value="Call on button 1"
             name="button2"
             onClick="clickFirstButton()">
    </form>    
    </html>

Event Handling in Java AWT Example

Ads









Related Tutorials/Questions & Answers:
Event listener in JavaScript with example
Event listener in JavaScript with example  What is event listener in JavaScript? Can anyone please post an example of Event listener?? Thanks in Advance
javascript event handling examples
javascript event handling examples  javascript event handling examples   var handleClick = function(e) { // Older IEs set the `event...; // For example's sake. if (target.tagName.toLowerCase() == 'a' && target.className
Advertisements
JavaScript onkeypress event
JavaScript onkeypress event...; This section illustrates you the use of onkeypress event in JavaScript. In the given example, we are going to show you how to restrict the user to enter any
Action Event Listener
Action Event Listener   ... listener for the component but second way permits you to include more than one event listeners. 1. Using "actionListener" attribute of the component
JavaScript Key Event
JavaScript onkeyup Event...; This section illustrates you the use of onkeyup event in JavaScript. In the given example, we are going to show you how to display the text of textField1
Need an Example of calendar event in java
Need an Example of calendar event in java  can somebody give me an example of calendar event of java
Value Change Event Listener
Value Change Event Listener   ... to use only one listener for the component but second way permits you to include... is called when the ValueChangeEvent action observed by this listener occurs. So
javascript recursion example
javascript recursion example   javascript recursion example   <html> <script> function factorial (n) { if(n==0) return(1); return (n * factorial (n-1) ); } document.write(factorial(5)); <
Flex terminating event flow example
Flex terminating event flow example       In the example below, how to terminate an event flow is demonstrated. With this example the motive of previous event phase detecting
JavaScript google map API example.
JavaScript google map API example.  How to Use Google Maps API in JavaScript?   Google Maps- A map is a way of representation of your route.... Initially Google introduced JavaScript maps API.Here is one example- <html>
Browser Back Button click event javascript
Browser Back Button click event javascript  I am using the below code but window.onBeforeUnload is not working with back button.. how can i apply the below code for the click event of back button? window.onBeforeUnload
Event Adapters
the implementation of all the methods of the listener interface. The following example... Event Adapters       There are some event listeners that have multiple methods to implement
Javascript
Javascript  How validations are done using javascript ,with example? and interview questions on javASCRIPT
Flex event phase detection example
Flex event phase detection example       With example below an introduction of phases in flex... regarding an event, such as detecting the event listener for handling
Event listeners in mxml
Event listeners in mxml  hi.. just tell me about How do you add event listeners in mxml components. Now AS3 components? Please give an example in both MXML and AS3 Thanks  Ans: There are some function
JavaScript
JavaScript  clone JavaScript
JavaScript createEventObject method
with the help of JavaScript. We can create an event object by using the JavaScript... JavaScript createEventObject method       If user wants to create and add any event
@WebListener Annotation Servlet Example
to implement a listener interface in servlet. In this example I will [email protected] Annotation Servlet Example In this tutorial you will learn what the difference is in the implementation of listener in servlet in older version
J2ME Event Handling Example
J2ME Event Handling Example       In J2ME programming language, Event Handling are used to handle certain... event like selecting an item from a list And low level events like pressing a key
javascript
javascript  javascript code to dynamically add table on button click.../javascript/javascriptexamples/javascript-add-row-dynamically.shtml http://www.roseindia.net/javascript/javascript-add-row-to-table.shtml http://www.roseindia.net
Javascript Form validation Example
Javascript Form validation Example In this section we will discuss about how to validate your application form in javascript. In many applications data... am giving a simple example for form validation using Javascript. Example
Event Handling In Java
that happens and your application behaves according to that event. For example... ) generates an event. This event is handled by the listener. Event listener listens... components has its own listener that handles the event generated by the component
JavaScript
should use JavaScript? Thanks   Hi, JavaScript is scripting language that runs on browser. You have to embed JavaScript in HTML page. Please see JavaScript tutorial. Thanks
JavaScript
JavaScript  how to get full path of a file type in javascript
javascript
javascript  Hi deepak, how to write form validation on javascript
javascript
javascript  write a program to display implement about browsers using javascript
javaScript
javaScript  How to open a browser window that cannot be resized? (HTML + Javascript
javascript
javascript  i have just learned javascript... can you tell me what kind of applications can be made by javascript?? thank you
JavaScript cloneNode example
JavaScript cloneNode example   .... JavaScript cloneNode() method creates a clone copy of the given node object...: In our example code for creating a clone of given node object we have created
javascript
javascript  how to set the request or session attribute in javascript head part of jsp page
javascript
javascript  Hi sir, This is sinduri, i want to learn javascript, so plz can u help me.how to learn

Ads