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

91
Visualizações
Send text instead of elements

I recently made a chat on my website, it works pretty good but i just found out that i can send elements there, like divs, anchors etc, how can i make it plain text?

thats how the code looks like

let msgDiv = '<div id="receivedMsgDiv">\n' +
                '<div id="rMsgName" class="messageName">\n' +
                    ''+data.val().name+'' +
                '</div>\n' +
                '<div id="receivedMsg" class="bounce">\n' + 
                    ''+data.val().message+'' +
                '</div>\n' +
                '<div id="rMsgTime">\n' +
                    ''+data.val().time+'' +
                '</div>\n' +
            '</div>';
let msg = document.getElementById("msgDiv");
msg.innerHTML += msgDiv;

i want a message to be a plain text so i can send eg "Hey friend i found the solution its: <div id="id">something</div>

instead of "Hey friend i found the solution, its: an element any ideas how to fix that?

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

0

Instead of using innerHTML, which causes the HTML parser to parse the string, use textContent which doesn't.

In the example below, I just made an input element to use as data since you didn't share what data was. I also used template literals to inject the dynamic values into the string.

let data = document.querySelector("input");
let newDiv = 
`<div id="receivedMsgDiv">
   <div id="rMsgName" class="messageName">${data.value}</div>
   <div id="receivedMsg" class="bounce">${data.value}</div>
   <div id="rMsgTime">${data.value}</div>
 </div>`;
document.getElementById("msgDiv").textContent += newDiv;
<input name="user" value="John Doe">
<div id="msgDiv"></div>

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