0

Is there any way you can add text to the beginning of a tag value like this.

<div id="idName">goes</div>
<script>
  document.getElementById("idName").innerHTML += " here.";
</script>

But instead of adding it to the end add it to the beginning. I already tried this.

<div id="idName">goes</div>
<script>
  document.getElementById("idName").innerHTML -= "Text ";
</script>
krillgar
  • 12,596
  • 6
  • 50
  • 86
Adam Oates
  • 193
  • 3
  • 12
  • You don't need to include [tags in titles](http://stackoverflow.com/help/tagging). – krillgar Feb 27 '16 at 23:49
  • Possible duplicate of [Concat to string at beginning](http://stackoverflow.com/questions/6094117/concat-to-string-at-beginning) – JJJ Feb 27 '16 at 23:50

3 Answers3

4

There must be a lot of ways to perform that, the easiest according to your example could be like this:

<div id="idName">goes</div>
<script>
  document.getElementById("idName").innerHTML = "Text " + document.getElementById("idName").innerHTML + " here.";
</script>
Gli
  • 163
  • 1
  • 8
1

  var idName = document.getElementById("idName").textContent;
document.getElementById("idName").textContent = " here." + idName;
<div id="idName">goes</div>
0

You can save idName innerHTML before add or change it .

<div id="idName">goes</div>
<script>
var xidName = document.getElementById("idName").innerHTML;
document.getElementById("idName").innerHTML =  "here " + xidName;
</script>