Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
How to make a certain part of textarea read-only

I have a text area and I dynamically add data to it and it works fine. What I wanted to achieve was after the data is appended that data cant be altered (edited) but after the last element of the data user can start typing on the textarea. I was thinking of maybe calculating the length of string data the set read-only to that part. How can I achieve this? Any help is appreciated. Thanks in advance.

For a visual example take a look at the terminal of this website: https://www.online-python.com/

function test() {
  x = 'this is a string to be appended to text area'
  document.getElementById("textArea").value = x;
}
<textarea id="textArea"></textarea>
<button onclick="test()">Append</button>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

It is not possible to selectively mark parts of a <textarea> read-only, however, a similar effect can be achieved with contenteditable elements:

function test() {
  const x = 'this is a string to be appended to text area'
  const span = document.createElement('span')
  span.appendChild(document.createTextNode(x))
  span.setAttribute('contenteditable', 'false')
  document.getElementById("textArea").appendChild(span);
}
#textArea{
  border: 1px solid black;
  height: 50px;
}
<div id="textArea" contenteditable="true"></div>
<button onclick="test()">Append</button>

However, this will still allow the user to delete the read-only block, or write before it.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can add a keydown event listener that checks whether the selectionStart is smaller than the length of the textarea's value minus the length of the string appended:

let x = 'this is a string to be appended to text area'
var hasAppended = false;

function test() {
  hasAppended = true
  document.getElementById("textArea").value = x;
}

textArea.addEventListener('keydown', function(e) {
  if (hasAppended) {
    if (this.selectionStart > this.value.length - x.length && this.selectionStart != this.value.length) {
      e.preventDefault()
      e.stopPropagation()
    }
  }
})
<textarea id="textArea"></textarea><button onclick="test()">Append</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

This almost works.

EDIT: Improved it a bit by changing keydown to keyup.

Now there is no need for space at the end of the read-only text and CTRL+a and then backspace will make the text come back almost instantly.

Maybe you can improve on it.

window.onload = function() {
  document.getElementById('textArea').addEventListener('keyup', function (e){
    var readOnlyLength = parseInt(document.getElementById("textArea").getAttribute('data-readOnlyLength') );
    var currentLength = parseInt(document.getElementById("textArea").value.length );
    if (readOnlyLength >= currentLength ) {
        document.getElementById("textArea").value = document.getElementById("textArea").getAttribute('data-readonly') ;
    }
  }, false);
};

function test() {
  x = 'this is a string to be appended to text area'
  document.getElementById("textArea").value = x;
  document.getElementById("textArea").setAttribute('data-readonly' , x); 
  document.getElementById("textArea").setAttribute('data-readOnlyLength' , x.length); 
}
<textarea id="textArea"></textarea>
<button onclick="test()">Append</button>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda