Question

I am working with ruby on rails and I want to test some things out.

I have used this simple code to change the innerHTML of my body:

document.body.innerHTML = "Work will you?";

It doesn't bring any results.

I know my javascript file works correctly because i tested it with the: alert() function

When i put the document.body.innerHTML into my file it breaks the javascript (the alert no longer works)

Anybody knows whats could cause this??

Was it helpful?

Solution

You must be running your code from within the <head> tag. document.body doesn't exist at that point. You have to wait for the DOM to be ready. You can do that using something like jQuery's $(document).ready() http://jsfiddle.net/L83mL/

Credit to https://stackoverflow.com/users/1013842/david and https://stackoverflow.com/users/400654/kevin-b

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top