Saturnix
Saturnix

Reputation: 10554

Can't change span content with JavaScript

// create text <span>
var text = document.createTextNode("Some content I want to replace... ");
var textspan = document.createElement("span");
textspan.id = "text" + counter;
textspan.appendChild(text)

div.appendChild(textspan);

// edit <span> content
document.getElementById("text" + this.index).innerHtml = 'hello';


console.log(document.getElementById("text" + this.index).innerHtml);
console.log(document.getElementById("text" + this.index));

result

> hello
> <span id=​"text0">​Some content I want to replace ​</span>​

InnerHtml changes correctly but the browser (and the console) still display the wrong content. Why is that?

Upvotes: 0

Views: 1905

Answers (3)

Jurijs Nesterovs
Jurijs Nesterovs

Reputation: 255

It would be better to use jquery $('#your_id').html('some kind of a text');, but if js suits you then innerHTML or innerText is used, as mentioned in other answers.

Upvotes: 0

jslayer
jslayer

Reputation: 484

You can change the content by setting your string to innerHTML or innerText Element properties. It is obviously what property for which there

Upvotes: 0

MaxArt
MaxArt

Reputation: 22637

It's innerHTML, not innerHtml.

Remember that in Javascript everything is case sensitive.

Upvotes: 4

Related Questions