I have a <textarea>
element, where users can input text. I would like to create a condition where only the last 10 characters written in the textarea can be edited.
For example user writes: My name is Joe.
In this case the user can edit only the following part: me is Joe.
.
I would also like it to be dynamic, meaning if user continues writing: My name is Joe. I am 30 years old.
, then the editable part would be: years old.
<div id="container">
<textarea id="inputText" name="inputText" wrap="soft"></textarea>
</div>
Does anyone have suggestions how to achieve this using JavaScript?
2
Answers
This can be done using javascript. You have to listen for the ‘input’ event of that textarea (and the onchange event also for solidity). When the user inputs you, have to save the state of that textarea into a variable in memory and then after each input you have to check if the first part (not allowed to edit) still matches, if not then replace the textarea content with your saved state.
Let me show a demo with some code:
Keep in mind that this code is only an idea, selecting a portion of the text and pasting things (not writing letter by letter) will make some weird behavior. You should consider covering that part too.
Try this: