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)