1

I am attempting to fire the standard submit event from recordEditForm by clicking a hidden button from javascript but the below code is not working for me any thoughts? I already attempted to use the Document.getElementById() so that I could use the base .click() event but the lightning button cannot have id as an attribute.

Aura:

<lightning:inputField fieldName="Sales_District_Lookup__c" onchange="{!c.onRecordSubmit}" />
<lightning:button 
           aura:id="submit"
           type="submit" 
           label="Save"  />

Javascript:

onRecordSubmit: function (component, event, helper) 
{   
    component.find('submit').click();
},
4
  • 1
    How do you know it's not working?
    – sfdcfox
    Commented Jan 2, 2019 at 21:36
  • 1
    @sfdcfox through the magic of testing and getting an error Commented Jan 2, 2019 at 22:10
  • 2
    @CalvinOKeefe I would not have asked had your question included the error message. When asking, please try to include error messages and other relevant data.
    – sfdcfox
    Commented Jan 2, 2019 at 22:16
  • @sfdcfox will do next time Commented Jan 2, 2019 at 22:16

2 Answers 2

4

Your lightning:inputField would be inside lightning base component like lightning:recordEditForm, why not directly call the submit method from your js onRecordSubmit?

<lightning:recordEditForm recordId="003XXXXXXXXXXXXXXX" objectApiName="MyObject__c" aura:id="myForm" >

    <lightning:inputField fieldName="Sales_District_Lookup__c" onchange="{!c.onRecordSubmit}" />
    <lightning:button 
               aura:id="submit"
               type="submit" 
               label="Save"  />
</lightning:recordEditForm>

JS code:

onRecordSubmit: function (component, event, helper) 
{   
    component.find('myForm').submit();
},

Src: https://developer.salesforce.com/docs/component-library/bundle/lightning:recordEditForm/documentation

1
  • This answer doesn't trigger the javascript onsubmit handler and directly just submit the form to server Commented Apr 16, 2021 at 21:47
0

You could just use a regular submit button which you can hide. I used this approach because the submit() method didn't fire the onsubmit of the recordEditForm.

Component code:

<button type="submit" name="submitForm" aura:id="submitForm" id="submitForm">Submit</button>

Submit code (controller / helper):

document.getElementById('submitForm').click();

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .