You need to sign in to do that
Don't have an account?
How can I remove or add attributes in LWC?
I'm trying to remove an attribute from an input field, specifically 'readonly' when I click on a button. I have tried this way and it is not working for me.
HTML
HTML
<lightning-input type="text" class="generalInfo" id="title" value="Title" readonly></lightning-input> <lightning-input type="text" class="generalInfo" value="Sub Title" readonly></lightning-input> <lightning-input type="text" class="generalInfo" value="Publication Date" readonly></lightning-input> <lightning-button-icon icon-name="utility:edit" alternative-text="Edit" title="Edit" onclick={editGeneralInfo}>editGeneralInfo() in JS
this.template.querySelectorAll(".generalInfo").forEach(element => { if(element.hasAttribute('readonly')){ console.log('YES'); element.removeAttribute('readonly'); } });Thank you!
I tired finding any such use cases that have been implemented previously but unfortunately I was not able to find any, however on checking I see that there are many such use cases implemented using lightning components.
On checking the documentation I found that the readonly attribute in lightning component is a boolean value however in LWC it is a attribute i.e., in LC:
Changes to
The only possible way was mentioned in this Stackexchange thread: https://salesforce.stackexchange.com/questions/272967/dynamically-adding-read-only-attribute-to-lightning-input-element-in-lwc-framewo
The usual way to address this is to hold the "read-only-ness" as tracked state (a change to it causes refresh of the UI) and to then conditionally render the input with or without the required read-only flag.
app.html
app.js
Also updated in your playground.
UPDATE:
I suspect the markup:
Can be replaced by:
Though I haven't tested it. The technique I showed is, however, good for other cases where you want to pass different parameters rather than just different parameter values.
I tried the above part after the UPDATE but unfortunately, it didn't work.
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks.
All Answers
Check below example of dynamically adding read-only attribute to lightning-input
https://salesforce.stackexchange.com/questions/272967/dynamically-adding-read-only-attribute-to-lightning-input-element-in-lwc-framewo
Thanks,
I tired finding any such use cases that have been implemented previously but unfortunately I was not able to find any, however on checking I see that there are many such use cases implemented using lightning components.
On checking the documentation I found that the readonly attribute in lightning component is a boolean value however in LWC it is a attribute i.e., in LC:
Changes to
The only possible way was mentioned in this Stackexchange thread: https://salesforce.stackexchange.com/questions/272967/dynamically-adding-read-only-attribute-to-lightning-input-element-in-lwc-framewo
The usual way to address this is to hold the "read-only-ness" as tracked state (a change to it causes refresh of the UI) and to then conditionally render the input with or without the required read-only flag.
app.html
app.js
Also updated in your playground.
UPDATE:
I suspect the markup:
Can be replaced by:
Though I haven't tested it. The technique I showed is, however, good for other cases where you want to pass different parameters rather than just different parameter values.
I tried the above part after the UPDATE but unfortunately, it didn't work.
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks.