Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

91
Vistas
Suppress rendering of html from template-literals within another template-literal

I'd like to display some html code that is within a template literals within another template literal.

I am using <pre><code> tags to attempt this.

The problem I'm having at the moment is that the browser renders the html rather than its code. For example it displays an actual input box in place of displaying the code, e.g. <input ...

I'd like to apply some reversible process using javascript to the templateliteral variable so that the html is displayed literally (it should display all orange on black text in the below snippet), and so that the original code can be easily got back.

What might be a good way to do this?

Here is an example of the problem (it should display all orange on black text):

let templateliteral = `let inner = { content : \`
<style>
#exampleDivInTemplateLiteral { background-color:lightblue;color:white; }
</style>

<div id="exampleDivInTemplateLiteral">
    <b>this is some text.</b>
    <input id="exampleInput" placeholder="example input">\</input>
</div>\`}`


document.getElementById("exampleDiv").innerHTML = '<pre><code>' + templateliteral + '<pre></code>'
<div id="exampleDiv" style="background-color:black;color:orange;">

<div>

Note this is specific to template literals within template literals.

Posting in edited form so I can post answer

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I used createElement and innerText (in place of innerHTML):

let templateliteral = `let inner = { content : \`
<style>
#exampleDivInTemplateLiteral { background-color:lightblue;color:white; }
</style>

<div id="exampleDivInTemplateLiteral">
    <b>this is some text.</b>
    <input id="exampleInput" placeholder="example input">\</input>
</div>\`}`

let divel = document.getElementById("exampleDiv")
let preel = document.createElement("pre");
let codeel = document.createElement("code");
preel.appendChild(codeel)
divel.appendChild(preel)
codeel.innerText =  templateliteral
<div id="exampleDiv" style="background-color:black;color:orange;">

<div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda