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.
![](https://mcusercontent.com/c5504cc750e8fae7e0e53dfd7/images/9c800416-b387-456d-81ba-eb7720d841fa.jpg)
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)