Suppose I have a select tag like below and I want to update its value through any attribute in javascript, then how can I achieve it because document.getElementById will not work in the lightning Web Components
<select id="demoSel" name="demoSel"> Suppose I have a select tag like below and I want to update its value through any attribute in javascript, then how can I achieve it because document.getElementById will not work in the lightning Web Components
<select id="demoSel" name="demoSel"> In order to access element in LWC you need to use
this.template.querySelector();
You cannot use ID in the above as the the IDs are generated dynamically when the component is rendered. But you can use other selectors namely the class and data-* attributes. For example if you add the class = "selectclass" to your select element, you can access it using
this.template.querySelector(".selectclass");
LWC reference for the above is at
the above link answered my question. To be specific I had to use conditional rendering inside the option tag just like this:
<option if:false={getSelectedOption}> and use getSelectedOption as getter in which particular object of the array is returned which has to be selected.