0

I have something like this:

var stopwatch = function (item) {
   window.setInterval(function () {
                item.innerHtml = myInteger++;
        }, 1000);
}

This code was supposed to display myInteger, but it is not updating values of item. Why (item is a div with text inside)?

1
  • 4
    Name of the property is innerHTML not innerHtml. Supposing item is a DOM Element object. Commented Apr 18, 2013 at 5:11

2 Answers 2

6

There could be a lot of reasons (we need to see more of your code), but here is a working example:

var myInteger = 1,
stopwatch = function (item) {
    window.setInterval(function () {
        item.innerHTML = myInteger++;
    }, 1000);
}
stopwatch(document.querySelector('div'));

Important changes:

  • Calling stopwatch (you probably do this)
  • - innerHtml +innerHTML (the case matters). You won't get an error for setting innerHtml; it will set that property and you won't notice anything.
  • Initialize myInteger.

http://jsfiddle.net/Q4krM/

Sign up to request clarification or add additional context in comments.

3 Comments

I have basically the same code as you do, yet it still doesn't work for me... I hate javascript with every inch of my body..
@ojek can you create an example on jsfiddle.net?
Dude don't tell me this works... It is not working on my site, ffs... Now I think that this is because the windows that I am using this on, is dynamically created? Is that it?
0

Try this code :

var stopwatch = setInterval(function (item) {
    item.innerHtml = myInteger++;
}, 1000);

This should work

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.