Understanding event.target.value in Salesforce LWC

Understanding event.target.value in Salesforce LWC
Chinmaya By Chinmaya
5 Min Read

Introduction

When working with Lightning Web Components (LWC) in Salesforce, you’ll often need to handle user input, such as text entered in an input field or a value selected from a dropdown.

The event.target.value property is a key part of handling these inputs. It allows you to capture the value of an input element when an event (like a change or input event) occurs.

In this blog post, we’ll break down what event.target.value is, how it works, and how to use it in your LWC components with a simple example.

What is event.target.value?

In LWC, event.target.value is a property that gives you the current value of an HTML input element (like a text field, checkbox, or dropdown) when an event is triggered. It is commonly used in event handlers to capture user input.

Key Points:

  1. event: Represents the event object that is automatically passed to the event handler function.

  2. target: Refers to the HTML element that triggered the event (e.g., an <input> or <select> element).

  3. value: Represents the current value of the element (e.g., the text entered in an input field or the selected option in a dropdown).

Where is event.target.value Used?

Where is event.target.value Used?

    1. Input Fields: To retrieve the text entered in a text box or text area.
    2. Dropdowns: To get the selected value from a picklist or dropdown.
    3. Checkboxes and Radio Buttons: To access the state (checked/unchecked) or value.

How Does event.target.value Work?

When a user interacts with an input element (e.g., types in a text field or selects an option from a dropdown), an event is triggered.
You can attach an event handler to the element to capture this event and access the value using event.target.value.

Example: Using event.target.value in LWC

Let’s create a simple LWC component that captures the value entered in a text input field and displays it on the screen.

Step 1: Create the HTML File

HTML (template):

				
					<!-- myInputComponent.html -->
<template>
    <lightning-card title="Input Example">
        <div class="slds-p-around_medium">
            <!-- Input field -->
            <lightning-input
                label="Enter your name"
                type="text"
                value={inputValue}
                onchange={handleInputChange}
            ></lightning-input>

            <!-- Display the entered value -->
            <p class="slds-m-top_medium">You entered: <strong>{displayValue}</strong></p>
        </div>
    </lightning-card>
</template>
				
			

Step 2: Create the JavaScript File

JavaScript (JS):

				
					// myInputComponent.js
import { LightningElement, track } from 'lwc';

export default class MyInputComponent extends LightningElement {
    @track inputValue = ''; // Holds the value entered in the input field
    @track displayValue = ''; // Holds the value to display

    // Event handler for the input field's change event
    handleInputChange(event) {
        // Capture the value using event.target.value
        this.inputValue = event.target.value;

        // Update the display value
        this.displayValue = this.inputValue;
    }
}
				
			

Step 3: Create the Metadata File

				
					<!-- myInputComponent.js-meta.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="myInputComponent">
    <apiVersion>57.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>
				
			

Explanation of the Example:

1. HTML File:

    • We use a <lightning-input> component to create an input field.

    • The onchange event is bound to the handleInputChange method in the JavaScript file.

    • The value attribute of the input field is bound to the inputValue property in the JavaScript file.

    • The entered value is displayed below the input field using the displayValue property.

2. JavaScript File:

    • The handleInputChange method is triggered whenever the user types in the input field.

    • event.target.value captures the current value of the input field.

    • The inputValue property is updated with the captured value.

    • The displayValue property is also updated to display the value on the screen.

3. Metadata File:

    • The component is exposed so it can be used in Lightning App Builder, Record Pages, and Home Pages.

How to Use event.target.value with Other Input Elements ?

event.target.value works with various input elements, such as:

1. Dropdown (Combobox)

HTML (template):

				
					<lightning-combobox
    label="Select an option"
    value={selectedValue}
    placeholder="Choose an option"
    options={options}
    onchange={handleComboboxChange}
></lightning-combobox>
				
			

JavaScript (JS):

				
					handleComboboxChange(event) {
    this.selectedValue = event.target.value;
}
				
			

2. Checkbox

HTML (template):

				
					<lightning-input
    type="checkbox"
    label="Agree to terms"
    checked={isChecked}
    onchange={handleCheckboxChange}
></lightning-input>
				
			

JavaScript (JS):

				
					handleCheckboxChange(event) {
    this.isChecked = event.target.checked; // Use `checked` for checkboxes
}
				
			

Key Points to Remember:

    1. event.target refers to the element that triggered the event.
    2. event.target.value is used to capture the value of input elements like text fields, dropdowns, etc.
    3. For checkboxes, use event.target.checked instead of event.target.value.

    4. Always bind the value attribute of the input element to a property in the JavaScript file to ensure two-way data binding.

    5. Ideal for handling user inputs dynamically in LWC components.

Conclusion

The event.target.value property is a simple yet powerful way to capture user input in Salesforce LWC. Whether you’re working with text fields, dropdowns, or checkboxes, understanding how to use event.target.value will help you build interactive and dynamic components.

By following the example in this post, you can easily implement user input handling in your LWC components. Keep experimenting with different input elements and events to enhance your Salesforce development skills!

Learn more tips like this on Writtee! 🚀

Share This Article
Follow:
Chinmaya is working as a Senior Consultant with a deep expertise in Salesforce. Holding multiple Salesforce certifications, he is dedicated to designing and implementing cutting-edge CRM solutions. As the creator of Writtee.com, Chinmaya shares his knowledge on educational and technological topics, helping others excel in Salesforce and related domains.
Leave a comment