![]() |
In AngularJS, we can get the original DOM element that fired the ng-click event using the event’s currentTarget property. Getting the event’s currentTarget can be useful when we want to track an element’s activity, in this case, whether and how many times a particular element is clicked, or we want to perform a certain operation if a particular element in the DOM gets clicked. In this article, we will see how to get the original element from ng-click in AngularJS. There are 2 approaches to get the original element by implementing the ng-click Directive: Table of Content Implementing the currentTarget PropertyIn this approach, we will use the currentTarget property of an event to get the original element that triggered the click event from the ng-click directive. The following steps will be followed:
Example: This example illustrates the basic implementation to get the original element from the ng-click Directive. HTML
Output: The output should log the original element that triggered the ng-click event to the console. Implementing the target PropertyIn this approach, we will use the target property of an event to get the element that triggered the click event. Here, we will create an <h1> and <p> element and on the click of the paragraph element, we will log the element to the console, as well as change its background color. Now, we will use the controller logic and modify it to use the target property of the event to get the target element on click of it and change the background color of the paragraph element. Example: This example demonstrates getting the original element from ng-click by implementing the target Property. HTML
Output: |
Reffered: https://www.geeksforgeeks.org
AngularJS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |