Base components are lightweight and closely resemble HTML markup. They follow standard HTML practices by providing event handlers as attributes, such as onfocus, instead of registering and firing Lightning component events, like components in the ui namespace.
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.
1({2 handleInputChange : function(component, event){3 let val = component.get("v.textvalue");4}5})
Alternatively, to retrieve the component that fired the event, use event.getSource().
1<aura:component>2 <lightning:button name="myButton" label="Click me" onclick="{!c.doSomething}"/>3</aura:component>
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.
1({2 doSomething: function(cmp, event, helper){3 var button = event.getSource();45 //Don’t use the following patterns for events6 //that are fired by another component 7 //or a base Lightning component 8 var el = event.target;9 var currentEl = event.currentTarget;10}11})
If Lightning Web Security is enabled in your org, event.currentTarget and event.target return the corresponding elements. For example, in the lightning:button example, both properties return the <button> element.
Note
Retrieve a component attribute that’s passed to the event by using this syntax.
1event.getSource().get("v.name")
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").
1({2 handleClick: function(cmp, event, helper){3 //returns "new", "edit", or "delete"4 var buttonName = event.getSource().get("v.name");5}6})
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.
1({2 handleActive: function(cmp, event){3 var tab = event.getSource();4 switch(tab.get('v.id')){5 case 'tab1':6 //do something when tab1 is clicked7 break;8 case 'tab2':9 //do something when tab2 is clicked10 break;11}12}13})
Retrieving the ID and Value Using the onselect Handler
Some components provide event handlers to pass in events to child components, such as the onselect event handler on the following components.
lightning:buttonMenu
lightning:tabset
Although the event.detail syntax continues to be supported, we recommend that you update your JavaScript code to use the following patterns for the onselect handler as we plan to deprecate event.detail in a future release.
event.getParam("id")
event.getParam("value")
For example, you want to retrieve the value of a selected menu item in a lightning:buttonMenu component from a client-side controller.
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.
1<aura:component>2 <aura:attribute name="value" type="String" default="Initial value"/>34 <!–- Don’t use event handlers that are not supported -->5 <lightning:input value="{!v.value}" onkeydown="{!c.handleKeyDown}"/>6</aura:component>
Since onkeydown is not a supported event handler based on the lightning:input specifications, event.getParam("value") and event.detail return undefined.