0

I have a setInterval and each time through the loop I want to add something to the beginning and end of the text in a div. Lets say I want to add the letter "x". I only know how to add to the end like this...

t=setInterval(function(){
document.getElementById('test').innerHTML +='x'
},1000)

Lets say the loop runs 10 times before I clear the interval, my text would look like,

My Textxxxxxxxxxx

I want it to look like

xxxxxxxxxxMy Textxxxxxxxxxx

1
  • 3
    document.getElementById('test').innerHTML="x"+document.getElementById('test').innerHTML +'x'; Commented Apr 16, 2015 at 1:56

4 Answers 4

5

Try the following code:

var tmpStr = document.getElementById('test').innerHTML;
tmpStr = 'a' + tmpStr + 'a';
document.getElementById('test').innerHTML = tmpStr;
Sign up to request clarification or add additional context in comments.

Comments

1

use the contents of the element directly

t=setInterval(function(){
  var contents = document.getElementById('test').innerHtml
  document.getElementById('test').innerHTML = 'x' + contents + 'x'
},1000)

Comments

1

Should just be able to save off the innerHTML:

t = setInterval(function(){
  var el = document.getElementById('test');
  var text = el.innerHTML;
  el.innerHTML = 'x' + text + 'x';
},1000)

Incidentally . . . this seems like a weird thing to want to do.

Comments

0
t=setInterval(function(){
var D = document.getElementById('test');
    D.innerHTML = "x"+D.innerHTML+"X";
},1000)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.