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

136
Vistas
How Do I Add an SVG to My Javascript .textContent?

I am simply trying to insert an exclamation SVG here, but I can't seem to do it, and I am unable to find an adequate answer on Google.

The SVG is downloaded, and contained within my project folder.

if(email.validity.valueMissing) {
        emailError.textContent = '(SVG Here) You need to enter an e-mail address.';
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Assuming e-mailError is a display element in your html (span, p, div etc.) the icon and associated text can be loaded by setting the .innerHTML property of the element.

The use of .textContent will result in the markup text being displayed rather than the intended layout (as you have found)

This working snippet demonstrates the difference.

const emailError=document.getElementsByClassName("emailError");

emailError[0].innerHTML = '<img src="https://stackoverflow.design/assets/img/logos/sf/sf-icon.svg" /> You need to enter an e-mail address.';

emailError[1].textContent = '<img src="https://stackoverflow.design/assets/img/logos/sf/sf-icon.svg" /> You need to enter an e-mail address.';
img {
width: 40px;
aspect-ratio: 1;
}

span {
border: 1px solid black;
display: flex;
align-items: center;
}
<p>Example loaded using .innerHTML:</p>
<p><span class="emailError"></span></p>

<p>Example loaded using .textContent:</p>
<p><span class="emailError"></span></p>

about 4 years ago · Santiago Trujillo 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