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?
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.
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>