2

This function uses jQuery to modify the contents of a DOM element. What am I doing wrong?

function updateScore() {
    alert("Test score is: " + bucket.score);
    $("#testScore").innerHTML = 'Current score is: + bucket.score';
}

The alert runs, but nothing else does. I have a <p> with the id testScore, but it doesn't change when I run the function. Why?

Thanks, Elliot Bonneville

Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123

3 Answers3

10

Try .html() on a jQuery object. innerHTML is for DOM-Elements.

$("#testScore").html('Current score is: '+ bucket.score);

If, for some reason, you really want to use innerHTML, you can convert the jQuery Object back to its DOM variant, for example using [0] or .get(0). Call like this, then:

$("#testScore")[0].innerHTML ='Current score is: '+ bucket.score

But I don't see why you would want to do that - since you're already writing in jQuery, there's no need to fallback to DOM methods that have a perfectly fine jQuery equivalent.

Community
  • 1
  • 1
Konerak
  • 39,272
  • 12
  • 98
  • 118
2
function updateScore() {
    alert("Test score is: " + bucket.score);
    $("#testScore").text('Current score is: ' + bucket.score);
}
aefxx
  • 24,835
  • 6
  • 45
  • 55
0

try

$("#testScore").innerHTML('Current score is: + bucket.score');

The jQuery objects do not support assignment to the attributes. (Its a Javascript limitation) You have to call the function with a parameter to set something.

Winston Ewert
  • 44,070
  • 10
  • 68
  • 83