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

128
Visualizações
How to force unbold from execCommand edited text

I am using document.execCommand to bold and underline text, but I want to make a button that forces the text to be unbolded and not underlined, rather than simply toggling. I can’t use execCommand(“removeFormat”) either, as it is causing issues in my program. Is there a work around I can use to unbold/un-underline the text?

window.addEventListener('keydown', function (event) {
  if (event.altKey){
    if (event.code === 'Digit1') {
// Unbold, unhighlight, un-underline // 
      document.execCommand("hiliteColor", false, "transparent")      
      changeFont('7.5pt') /* This goes into a function to change the fontsize */
  }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you want to remove all formatting, you can easily do it by replacing the HTML with its text equivalent:

const editor = document.getElementById("editor");
document.getElementById("unformat").addEventListener("click", () => {
  editor.innerHTML = editor.innerText;
});
<div id="editor" contenteditable>Mess <u>with me</u>, and <b>add crazy</b> formatting <b>to your heart&apos;s <u>content</u></b> then click <code>UNFORMAT</code></div>
<button id="unformat">UNFORMAT</button>

If you want to only remove bolded text, you could replace the bold nodes with text, like this: If you want to individually unformat something, you could do it like this (example with bolding):

const editor = document.getElementById("editor");
document.getElementById("unbold").addEventListener("click", () => {
  // get all bolded elements
  Array.from(editor.getElementsByTagName("b")).forEach(i => {
    i.outerHTML = i.innerHTML; // replace the bold with its contents
  });
});
<div id="editor" contenteditable>Mess <u>with me</u>, try <b>bolding</b> random <b>parts <u>then</u></b> click <code>UNBOLD</code></div>
<button id="unbold">UNBOLD</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