ojek
ojek

Reputation: 10068

Javascript - how to update elements inner html?

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

Upvotes: 0

Views: 129

Answers (2)

Vicky Gonsalves
Vicky Gonsalves

Reputation: 11707

Try this code :

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

This should work

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191729

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/

Upvotes: 6

Related Questions