function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sagar 12Sagar 12 

Difference between component.get() and component.find()

I am a beginner in salesforce lightning and trying to create few components.
Can someone explain to me
1) what is the difference between 'id' and 'aura:id' attribute?
2) when to use component.get() and component.find() ?
 
Best Answer chosen by Sagar 12
Raj VakatiRaj Vakati
See the below example .. 

 
<aura:component >
    <aura:attribute name="property" type="String" default="Demo"/>
    <aura:handler name="init" value="this" action="{!c.doInit}"/>
    <ui:inputText aura:id="inputComponent" class="slds-input" value="{!v.property}" />
    <ui:inputText aura:id="auraid" class="slds-input"/>
    
</aura:component>


({
    doInit : function(component, event, helper) {
        component.find("auraid").set("v.value", "Find Example");
        component.set("v.property", "Set Ex");
         let val = component.get("v.property"); 
    },
    
})

 

All Answers

Raj VakatiRaj Vakati

component.find() :

Retrieve a component by its ID in JavaScript code. component.find() returns different types depending on the result.
Locates a component using the localId. Shorthand: get("asdf"), where "asdf" is the aura:id of the component to look for.
Returns different types depending on the result.
  • If the local ID is unique, find() returns the component.
  • If there are multiple components with the same local ID, find() returns an array of the components.
  •  If there is no matching local ID, find() returns undefined.
  •  Returns instances of a component using the format cmp.find({ instancesOf : "auradocs:sampleComponent" }).
// returns the instance(s) of type Aura.Component
 var cmpInstance = component.find("auraid")


component.get()

Returns the value referenced using property syntax. For example, cmp.get("v.attr") returns the value of the attr aura:attribute.
 
<aura:attribute name="totalPrice" type="Integer" default="0"/>


In controller 


cmp.set("v.totalPrice")

 
Raj VakatiRaj Vakati
https://salesforce.stackexchange.com/questions/171231/component-find-vs-auraattribute
Sagar 12Sagar 12
Do you have any example code where I can check this by executing the code?
Raj VakatiRaj Vakati
See the below example .. 

 
<aura:component >
    <aura:attribute name="property" type="String" default="Demo"/>
    <aura:handler name="init" value="this" action="{!c.doInit}"/>
    <ui:inputText aura:id="inputComponent" class="slds-input" value="{!v.property}" />
    <ui:inputText aura:id="auraid" class="slds-input"/>
    
</aura:component>


({
    doInit : function(component, event, helper) {
        component.find("auraid").set("v.value", "Find Example");
        component.set("v.property", "Set Ex");
         let val = component.get("v.property"); 
    },
    
})

 
This was selected as the best answer
Raj VakatiRaj Vakati
Is it is understandable? close this thread if its solved 
Sagar 12Sagar 12
Thanks, Now it is clear.