0

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?

j08691
  • 204,283
  • 31
  • 260
  • 272

2 Answers2

1

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.

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> 
Ran Turner
  • 14,906
  • 5
  • 47
  • 53