user2527750
user2527750

Reputation: 51

Javascript not updating text within a div

I must be daft, but the javascript is not changing the containing div text as I would expect once the div is clicked:

favourite.onclick = function() {     
        loadXMLDoc('indexFavourite'); 
        var linkclass = favourite.className;
        if(linkclass == 'favouriteOption')
            favourite.className = 'favouriteOptionActive',
            favourite.className.update("New text");
        else
            favourite.className = 'favouriteOption';
}

Upvotes: 0

Views: 270

Answers (3)

Johan
Johan

Reputation: 8266

favorite.text('New text') will set the text

note this will work if using jQuery, my bad!

Upvotes: 0

mplungjan
mplungjan

Reputation: 177950

Your syntax is way off, missing bracket and whatnot

favourite.onclick = function() {     
  loadXMLDoc('indexFavourite'); 
  var linkclass = favourite.className;
  if(linkclass == 'favouriteOption') {
    favourite.className = 'favouriteOptionActive',
    favourite.innerHTML="New text";
  }
  else {
    favourite.className = 'favouriteOption';
  }
}

Upvotes: 1

Krasimir
Krasimir

Reputation: 13529

What you are doing here is changing the class of a div (probably). And even this is kinda wrong. I mean

favourite.className = 'favouriteOptionActive',
favourite.className.update("New text");

should actually produce an error, because the string favouriteOptionActive doesn't have a method update. It could have only if you patch the String.prototype.

If you want to change the div's text you should use div.innerHTML or div.innerText.

Upvotes: 0

Related Questions