You need to sign in to do that
Don't have an account?
szesze20
Lightning Component question: not clearing field upon clicking button
I just started playing with Lightning and have created controller and cmp. Intended behavior:
Here's my component code:
<aura:component implements="force:appHostable" access="global">
<ui:inputText aura:id="name" label="Find TravelingSpoon:"/>
<ui:button aura:id="button" buttonTitle="Submit Input" class="button" label="Submit" press="{!c.getInput}"/>
<ui:outputText aura:id="outName" value="" class="text"/>
<ui:button aura:id="button" buttonTitle="Clear the field" class="button" label="Clear" press="{!c.clearName}"/>
</aura:component>
Here's my controller code:
({
getInput : function(cmp, evt) {
var myName = cmp.find("name").get("v.value");
var myText = cmp.find("outName");
var greet = "Hi, " + myName;
myText.set("v.value", greet);
cmp.set("v.value", []);
}
})
({
clearName : function(cmp, evt) {
cmp.find("outName").set("v.value", "");
}
}
)
- User enters text in 'Find Travelingspoon' field, click Submit, it displays text 'Hi, [text]' - this part works fine.
- When user clicks 'Clear button on Salesforce1, it clears whatever text is in the 'Find Traveling Spoon' text box, but it's not clearing out the text, the text stays in the box.
Here's my component code:
<aura:component implements="force:appHostable" access="global">
<ui:inputText aura:id="name" label="Find TravelingSpoon:"/>
<ui:button aura:id="button" buttonTitle="Submit Input" class="button" label="Submit" press="{!c.getInput}"/>
<ui:outputText aura:id="outName" value="" class="text"/>
<ui:button aura:id="button" buttonTitle="Clear the field" class="button" label="Clear" press="{!c.clearName}"/>
</aura:component>
Here's my controller code:
({
getInput : function(cmp, evt) {
var myName = cmp.find("name").get("v.value");
var myText = cmp.find("outName");
var greet = "Hi, " + myName;
myText.set("v.value", greet);
cmp.set("v.value", []);
}
})
({
clearName : function(cmp, evt) {
cmp.find("outName").set("v.value", "");
}
}
)
The correct approach is to change your declaration style and the set statements to
thanks Gaurav. I followed your advice and modified to the following, however, it's still *not* clearing the text in 'Find TravelingSpoon' text box when I click 'Clear' button. Please advise.
My updated component and controller code is below:
({
getInput : function(cmp, evt) {
var myName = cmp.find("name").get("v.value");
var myText = cmp.find("outName");
var greet = "Hi, " + myName;
myText.set("v.value", greet);
cmp.set("v.value", []);
}, //Put comma here
clearName : function(cmp, evt) {
cmp.find("outName").set("v.value", "");
}
})
For Example: This was the id for my components input field : "SearchManager_typeaheadlookup" And now on the controller use the following code to set the value to null.