Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

89
Vistas
How do I add text last in a text element in html via js

I want to add text last in a p element, not first, but i only know how to add last using +=

var pEL = document.querySelector("#text");
pEL.innerHTML += "1";
<p id="text">text</p>

now the p element says "text1" but i want "1text", how do i add something first in a text element?

7 months ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0


pE1.innerHTML += "1";  

is simply shorthand for saying "the new html will be the old html + "1"

pEl.innerHTML = pEl.innerHTML + "1"

so instead, something like


pEL.innerHTML = "1" + pEl.innerHTML   

should do the trick.

7 months ago · Juan Pablo Isaza Denunciar

0

The Element property innerHTML gets or sets the HTML or XML markup contained within the element so set it with concatenating 1 + the previous text (calling pEL.innerHTML without assignment is basiclly using the get to fetch the value)

var pEL = document.querySelector("#text");
pEL.innerHTML = "1" + pEL.innerHTML;
<p id="text">text</p> 

7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos