user2089120
user2089120

Reputation: 195

.innerHTML not working

I have a very stupid problem. I would like to change the content of a table cell. My code:

        var x = ajax('...');

    alert(x);
    alert(document.getElementById(tdId).innerHTML);

    document.getElementById(tdId).innerHTML = x;

"x" has the right content => so ajax is working And the second alert(...) gives also the right (actual) content. But when I would like to write x in the cell, nothing happens. No error, nothing...

I checked some similar problems here, but without success.

I have a realy no idea, what can I test further to understand the problem. Do you have an idea? Thanks a lot for your help!

Upvotes: -1

Views: 16252

Answers (3)

user2089120
user2089120

Reputation: 195

I've got the solution!!!

This is very stupid: I changed the content of the td by clicking on the td... but the new content has a button to click again... now, firefox starts from the beginning on and shows the "second" content again...

Thats hard! :-)

However, I realy thank you very very much for your help! Sorry for wasting your time! :(

Upvotes: 0

Rafael Herscovici
Rafael Herscovici

Reputation: 17094

var x = "Hello World";
var td = document.getElementById("myId");
var text = document.createTextNode(x);
td.appendChild(text);

if your script is on the top of the page, the DOM has not finished loading when you try to run your javascript function.

make sure your Javascript is running after the DOM has loaded:

window.onload += function(){
    var x = "Hello World";
    var td = document.getElementById("myId");
    var text = document.createTextNode(x);
    td.appendChild(text);
}; 

or, move your script file to the bottom of the page. (the first is more recommened)

Upvotes: 0

Victor Ferreira
Victor Ferreira

Reputation: 6449

your code is correct, but make sure this element is not hidden (with display: none or visibility: hidden, for example). if you set a default value in the HTML file, will it appear?

you can also make a test adding the content of x to another element. try document.body.innerHTML += x or even replace x to a string literal such as "this is a test".

Upvotes: 0

Related Questions