Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to trigger an event using Javascript "addEventListener()" method.

    • 0
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 302
    Comment on it

    Hello Readers,

    Their are many ways in javascript and jquery where we can trigger an event using Id or Class. Here's the example of how we can trigger the event using "addEventListener()" method.

    Let's suppose we have have button having id="myBtn".

    <button id="myBtn">Try it</button>
    
    <script>
    document.getElementById("myBtn").addEventListener("click", displayDate);
    
    function displayDate() {
        document.getElementById("demo").innerHTML = Date();
    }
    </script>
    

    When id="mybtn" is call it will trigger click function of addEventListener where it will redirect to displayDate function and display the current date and time in innerHtml of "demo" id.

    <p id="demo"></p>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: