How to reset lightning input field values on button click dynamic JavaScript method function Uses of “lightning-record-edit-form” & “lightning-input-field” in Lightning Web Component — LWC | How to clear lightning-input on click in lwc

7,184 views

Hey guys, today in this post we are going to learn about How to reset lightning input field value on button click dynamic JavaScript method function Uses of “lightning-record-edit-form” & “lightning-input-field” in Lightning Web Component — LWC.

This example we creates a form with six fields, When you click the Reset Form Button, the resetFormAction method will call and resets their initial values.

We Uses of “lightning-record-edit-form” & “lightning-input-field” element to create a form and reset() method to reset the form fields values.

Challenges in your career

🎯 If You Are Facing Any Of These 6 Challenges in your career.

  • Learn Salesforce Development
  • Career Confusion
  • No Interview Call
  • Low Salary
  • No Promotion/Growth
  • No Finding New Job Opportunity
  • Why you stucking from past so many years in same company?

 

Files we used in this post example

lwcResetForm.html LWc HTML File Template HTML file to reset the lightning input field values in Salesforce Lightning Web Component (LWC)
lwcResetForm.js LWC JavaScript File It’s hold a javascript on resetFormAction function to reset the lightning input field values on click button.
lwcResetForm.js-meta.xml XML Meta File It is used to where this lightning web component file you want to display as like lightning__AppPage, lightning__RecordPage, lightning__HomePage.

Final Output

How to reset lightning input field values on button click in LWC -- w3web.net

 

You can download file directly from github by Click Here.

 
 

Other related post that would you like to learn in Salesforce

Find the below steps:-

Create Lightning Web Component HTML

Step 1:- Create Lightning Web Component HTML ➑ lwcResetForm.html

SFDX:Lightning Web Component ➑ New ➑ lwcResetForm.html

lwcResetForm.html [Lightning Web Component HTML]

  1.   <template>
  2.     <lightning-card title="How to reset the form dynamically in Lightning Web Component -- LWC" icon-name="custom:custom15" size="small">
  3.        <div class="slds-p-around_medium">
  4.  
  5.         <lightning-record-edit-form record-id={recordId}  object-api-name={objectApiName}>
  6.             <div class="slds-grid slds-wrap" style="background:#eee; border:1px #ddd solid; padding:10px;">
  7.                 <div class="slds-col slds-size_6-of-12 slds-p-horizontal_medium">
  8.                     <lightning-input-field field-name="FirstName"></lightning-input-field>
  9.                 </div>
  10.                 <div class="slds-col slds-size_6-of-12 slds-p-horizontal_medium">
  11.                     <lightning-input-field field-name="LastName"></lightning-input-field>
  12.                 </div>
  13.                 <div class="slds-col slds-size_6-of-12 slds-p-horizontal_medium">
  14.                     <lightning-input-field field-name="Email"></lightning-input-field>
  15.                 </div> 
  16.                 <div class="slds-col slds-size_6-of-12 slds-p-horizontal_medium">
  17.                     <lightning-input-field field-name="Phone"></lightning-input-field>
  18.                 </div>
  19.                 <div class="slds-col slds-size_6-of-12 slds-p-horizontal_medium">
  20.                     <lightning-input-field field-name="Title"></lightning-input-field>
  21.                 </div>
  22.                 <div class="slds-col slds-size_6-of-12 slds-p-horizontal_medium">
  23.                     <lightning-input-field field-name="Department"></lightning-input-field>
  24.                 </div>
  25.             </div>
  26.  
  27.  
  28.             <div class="slds-text-align--center slds-m-top_medium">
  29.                 <lightning-button variant="destructive" title="Reset Form" class="slds-m-top_small slds-m-horizontal_small" label="Reset Form" onclick={resetFormAction}></lightning-button>                
  30.                 <lightning-button variant="brand" disabled={disabledButton} class="slds-m-top_small" type="submit" label="Save Record"></lightning-button>
  31.             </div>
  32.         </lightning-record-edit-form>
  33.     </div>  
  34. <br/><br/>
  35.    <!--Start RelatedTopics Section-->
  36. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  37.  
  38.             <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click  <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p>
  39.  
  40.             <br/><br/>
  41.             <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like β†’</span> </strong></p>
  42.             <div style="display:block; overflow:hidden;"> 
  43.                 <div style="width: 50%; float:left; display:inline-block">
  44.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  45.                         <li><a href="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
  46.                         <li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li>
  47.                         <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li>
  48.                         <li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li>
  49.                         <li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li>
  50.                     </ul>
  51.             </div>
  52.  
  53.             <div style="width: 50%; float:left; display:inline-block">
  54.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  55.                         <li><a href="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li>
  56.                         <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li>
  57.                         <li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li>
  58.                         <li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li>
  59.                         <li><a href="https://www.w3web.net/update-parent-object-from-child/" target="_blank" rel="noopener noreferrer">update parent field from child using apex trigger</a></li>
  60.                     </ul>
  61.                 </div>
  62.                <div style="clear:both;"></div> 
  63.                <br/>
  64.                 <div class="youtubeIcon">
  65.                     <a href="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a>
  66.                 </div>
  67.     </div>
  68.  
  69. </div>
  70.  
  71.   <!--End RelatedTopics Section-->
  72.     </lightning-card>
  73. </template>

Create Lightning Web Component Javascript

Step 2:- Create Lightning Web Component Javascript ➑ lwcResetForm.js

SFDX:Lightning Web Component ➑ New ➑ lwcResetForm.js

lwcResetForm.js [LWC JavaScript File]

  1.   import { LightningElement, api } from 'lwc';
  2.  
  3. export default class LwcResetForm extends LightningElement {
  4.     @api recordId;
  5.     @api objectApiName='Contact'; 
  6.     @api disabledButton;
  7.     disabledButton=true;
  8.  
  9.     resetFormAction(event) {
  10.         const lwcInputFields = this.template.querySelectorAll(
  11.             'lightning-input-field'
  12.         );
  13.         if (lwcInputFields) {
  14.             lwcInputFields.forEach(field => {
  15.                 field.reset();
  16.             });
  17.         }
  18.      }
  19. }

Create Lightning Web Component Meta XML

Step 3:- Create Lightning Web Component Meta XML ➑ lwcResetForm.js-meta.xml

SFDX:Lightning Web Component ➑ New >> lwcResetForm.js-meta.xml

lwcResetForm.js-meta.xml [LWC Meta Data XML]

  1.    <?xml version="1.0" encoding="UTF-8"?>
  2. <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
  3.     <apiVersion>45.0</apiVersion>
  4.     <isExposed>true</isExposed>
  5.     <targets>
  6.         <target>lightning__AppPage</target>
  7.         <target>lightning__RecordPage</target>
  8.         <target>lightning__HomePage</target>
  9.       </targets>
  10. </LightningComponentBundle>

Create Lightning Application

Step 4:- Create Lightning Application : lwcResetFormApp.app

From Developer Console >> File >> New >> Lightning Application

lwcResetFormApp.app [Component Application File]

  1.    <aura:application extends="force:slds">
  2.     <c:lwcResetForm/>
  3. </aura:application>

reset lightning input field on button click in lwc -- w3web.net

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

How do you reset lightning input?

To reset the form fields to their initial values, use the reset() method on lightning-input-field . We recommend creating a button that reverts the field values to their initial values using lightning-button.

How do you clear the lightning input field after submitting?

As per lightning Input documentation, there is no reset method available as part of component. If you are using the lightning input and want to reset option put a button click.

How do I force a refresh page in LWC?

You will need to import the updateRecord method from the lightning/uiRecordApi . Then, you can call this updateRecord() function in your lwc javascript file whenever you want your detail record page to refresh.

Related Topics | You May Also Like

 
  

Our Free Courses β†’

πŸ‘‰ Get Free Course β†’

πŸ“Œ Salesforce Administrators

πŸ“Œ Salesforce Lightning Flow Builder

πŸ“Œ Salesforce Record Trigger Flow Builder

πŸ‘‰ Get Free Course β†’

πŸ“Œ Aura Lightning Framework

πŸ“Œ Lightning Web Component (LWC)

πŸ“Œ Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment