I want to add a variable like this. Day value is an Integer

var day = component.find('days').get('v.value');
var date1 = component.find('HDate'+day+'__c').get('v.value');

But it returns an error at get(). How can I add a variable inside a find markup

Thanks in advance :)

  • 2
    Without knowing more, we can't really help you. At first glance, this code looks okay. Most likely, you're either finding too many/too few elements, or the name is wrong. Please feel free to edit your question to include any relevant markup from your component. – sfdcfox Dec 19 '18 at 11:13
  • Ok i will try to write in detail markup next time @sfdcfox as Always thanks man. – Jayee Dec 26 '18 at 8:33

It's unlikely that this is correct:

var date1 = component.find('HDate'+day+'__c').get('v.value');

because it appears to imply that your component is either iterating and generating a list of elements with aura:ids like HDate1__c and HDate2__c, or that the aura:id of this component is dynamically derived from another component attribute.

Since Aura Ids cannot be dynamic in Lightning component markup and you haven't shown us any indication you're creating components dynamically in JavaScript, I think it's likely that you are mistaken about what the actual aura:id values of the elements in your component are, and that component.find() is not returning you a component.

More broadly, component.find() can return undefined, a single component, or n array of components, and you need to be prepared to handle those return values based on your component markup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.