Andrew Diamond
Andrew Diamond

Reputation: 6335

Need help updating some text repeatedly in Javascript

So I have this code in the body of my HTML,

<div id="timer">
 <div id="years">## Years</div>
 <div id="months">## Months</div>
 <div id="days">## Days</div>
 <div id="hours">## Hours</div>
 <div id="minutes">## Minutes</div>
 <div id="seconds">## Seconds</div>
 <div id="milliseconds">## Milliseconds</div>
</div>

and I would like to replace the "##" with the values of some variables that I have set up, but have them be replaced as the variables change, how would I go about doing this with Javascript?

Upvotes: 2

Views: 801

Answers (1)

laaposto
laaposto

Reputation: 12213

You try this

var years="12";    
document.getElementById("years").innerHTML=document.getElementById("years").innerHTML.replace("##",years);

var months="11"
document.getElementById("months").innerHTML=document.getElementById("months").innerHTML.replace("##",months);

And with the same way for the other elements

DEMO

UPDATE:

For repeatedly you will have to do something like this

var seconds=0;
var inner=document.getElementById("seconds").innerHTML;
setInterval(function(){

document.getElementById("seconds").innerHTML=seconds+inner.substring(2);
    seconds+=1;
    },1000);

DEMO2

Upvotes: 4

Related Questions