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

207
Visualizações
Script only changing document.body.contentEditable to false and not true
javascript:if(document.body.contentEditable == false){document.body.contentEditable = true;}else{document.body.contentEditable = false;} void 0;

it appears that all it does is make it false.

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

0

Since contenteditable is an element attribute, it is best read, set, or changed, using the .getAttribute and .setAttribute properties of the element's object.

The snippet shows the syntax used to read and set the attribute, using a working example where a button toggles editability of a paragraph element.

let editBox = document.getElementById("edit-field");

document.getElementsByTagName('button')[0].addEventListener('click', event =>{

    if (editBox.getAttribute("contenteditable")=="true") {
    editBox.setAttribute("contenteditable", "false");
    console.log("switched to not editable");
  } else {
    editBox.setAttribute("contenteditable", "true");
    console.log("switched to editable");
  }
}) // end button click;
#edit-field {
  width: 300px;
  height: 100px;
  background: yellow;
}
<p id="edit-field" contenteditable="false"> I am editable (sometimes)</p>
<button>toggle editability </button>

about 4 years ago · Juan Pablo Isaza Relatório

0

The reason it is always changing it to false is primarily because document.body.contentEditable is going to be a string (i.e., "true", "false", "inherit"). Your code just needs to include quotes around true and false in your conditional for it to work as intended:

javascript:if(document.body.contentEditable == "false"){document.body.contentEditable = "true";}else{document.body.contentEditable = "false";} void 0;
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