In LWC there are multiple ways to dynamically set values to custom or standard components. Here we have discussed two ways to set value to lightning-combobox component and lighting-input.
Way 1: Is using Query Selector. In the below code we set value to lightning-input by using QuerySelector.
Way 2: With visibility. As you can see in below code we set value to second combobox, which is selected from first combobox. In handleChange method we hide the second combox and set value and then make it visible again.
HTML File
JS File
import { LightningElement, track } from 'lwc';
export default class PicklistDemo extends LightningElement {
@track setValue = true;
value = '';
get options() {
return [
{ label: 'India', value: 'India' },
{ label: 'USA', value: 'USA' },
{ label: 'Japan', value: 'Japan' },
];
}
handleChange(event) {
this.value = event.detail.value;
this.template.querySelector('lightning-input.customClass').value = this.value;
this.setValue = false;
this.value = this.value;
this.setValue = true;
}
}