Newer Version Available
Event Handling in Lightning Base Components
Because of their markup, you might expect to access DOM elements for base components via event.target or event.currentTarget. However, this type of access breaks encapsulation because it provides access to another component’s DOM elements, which are subject to change.
Use the methods described here to make your code compliant with Lightning Locker.
We recommend binding your value to an attribute. For example, bind the value for lightning:input to a textvalue attribute.
In your client-side controller, use the event handler to get the textvalue attribute value.
Alternatively, to retrieve the component that fired the event, use event.getSource().
For an event fired by a custom component or a base component, use event.getSource(). For events fired by standard HTML elements, you can use event.currentTarget and event.target. For example, event.target returns null when the lightning:button component in the example is clicked.
Reusing Event Handlers
event.getSource() helps you determine which component fired an event. Let’s say you have several buttons that reuse the same onclick handler. To retrieve the name of the button that fired the event, use event.getSource().get("v.name").
Retrieving the Active Component Using the onactive Handler
When working with tabs, you want to know which one is active. The lightning:tab component enables you to obtain a reference to the target component when it becomes active using the onactive handler. Clicking the component multiple times invokes the handler one time only.
Retrieving the ID and Value Using the onselect Handler
- lightning:buttonMenu
- lightning:tabset
- event.getParam("id")
- event.getParam("value")
Base Components Limitations for Native HTML Events
All supported event handlers on a base component are listed in the Specification tab of the component reference. The event handler names start with on, for example, onchange.
Base components generally don’t support native HTML events, unlike their Lightning web component counterparts. You might encounter unexpected behavior if you try to handle an HTML event on a base component. Let’s say you want to handle the onkeydown HTML event on lightning:input.
Since onkeydown is not a supported event handler based on the lightning:input specifications, event.getParam("value") and event.detail return undefined.