Based on the selection of a radio button, I would like to toggle the read-only
attribute of lightning-input
element. setAttribute
method on DOM element doesn't seem to work.
app.html
<template>
<div class="slds-grid">
<div class="slds-col">
<lightning-radio-group name="opportunityCreation"
label="Create an Opportunity"
options={options}
value={createOpp}
onchange={createOppHandler}>
</lightning-radio-group>
</div>
<div class="slds-col">
<lightning-input label="Opportunity Name" data-id="oppName">
</lightning-input>
</div>
</div>
</template>
app.js
import { LightningElement, track, api } from 'lwc';
export default class App extends LightningElement {
@track options = [{'label': 'Yes', value:'yes'},{'label':'No', value:'no'}];
@track createOpp = 'yes';
createOppHandler(event){
this.createOpp = event.detail.value;
let inputTextElem = this.template.querySelector("[data-id='oppName']");
if(this.createOpp === 'no'){
inputTextElem.setAttribute("read-only", "");
}
else{
inputTextElem.removeAttribute("read-only");
}
}
}