Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Pass Element ID that Triggers `onclick` Event to the Event Handling Function

    • 0
    • 1
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 6.52k
    Comment on it

    When we apply onclick event to an element, sometimes we need to pass ID of that element to the event handling function. We can do this by passing id to function as this.id or we can pass the element itself.

    Example: In the below example I've created tow examples to pass element itself and only id.

     <!doctype html>
        <html lang="en">
        <head>
        <meta charset="utf-8">
        <title>Demo</title>
        <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
        </head>
        <body>
        <button id="startButton" onClick="checkElement(this);">Start</button>
        <button id="stopButton" onClick="checkId(this.id);">Stop</button>
        <script>
    	function checkElement (e) {
    	   alert(e.id);      
    	}
    	function checkId (id) {
    	   alert(id);      
    	}
        </script>
        </body>
        </html>
    
    

     

    Hope this will help you :)

    In case any doubt please feel free to ask in comments below.

 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: