2

I have very simple code

html

<div id="ball" class="ball"></div>

css

#ball{
    width: 20px;
    height: 20px;
    border-radius: 100%;
    background: #0f0;
    position: absolute;
    bottom: 150px;
    left: 350px;
}

javascript

<script type="text/javascript">
    document.getElementById('ball').style.backgroundColor="red";
</script>

I tried this code in jsfiddle and it works but why this is not working locally? I tried changing to , but I've no idea why this is not working. It's showing the error:

TypeError: document.getElementById(...) is null

6
  • I know I can do it with jquery very easily but wanted to know with javascript... and I'm first to javascript. Commented Dec 2, 2013 at 7:23
  • 5
    Most likely you're executing the script before the element exists. Commented Dec 2, 2013 at 7:23
  • script is in head tag and html is in body tag . so how to do? Commented Dec 2, 2013 at 7:25
  • 1
    also see Code works in fiddle, but not on webpage Commented Dec 2, 2013 at 7:27
  • Show your full HTML page code. Commented Dec 2, 2013 at 7:29

1 Answer 1

3

As mentioned in the comment, your initialization order is messed up. You can use window.onload to fix it:

<script type="text/javascript">
    window.onload = function() {
        document.getElementById('ball').style.backgroundColor="red";
    };
</script>

Note also that you can only have one onload trigger function. Here are some ideas on how to support multiple trigger functions. The easiest, is of course, to just use JQuery's ready function.

Another way is to put the <script> tag into the body after the DOM element(s) that it depends on. This is often encouraged for performance reasons, but it tends to make things less readable.

The reason as to why <script> tags in the head are executed in sequence (instead of delaying execution until after the DOM has loaded) is: Performance. For example, you sometimes might want to start asynchronous requests before the document has finished loading. If you are not concerned about performance, it's safer to execute them "onload".

Ultimately, there are a lot of considerations to be made when placing your <script> tag. If you want to learn more, this might be a good starting point.

About the second part of your question: The reason as to why it works in JSFiddle is that, by default, it executes scripts onload. You can reproduce the bug on JSfiddle by choosing "no wrap - in head": http://jsfiddle.net/aDuwg/.

Sign up to request clarification or add additional context in comments.

9 Comments

thanks. its working. so it means whatever I code in js should be within onload?
@C-Link No, just what needs to be load first.
@C-Link: The only rule is that the element you are trying to access has to exist. There are multiple ways to achieve that, this is one of them.
You can include the script tag in the body of HTML after all of your elements and it should work there too. In fact, many people recommend putting all your script tags at the bottom of the HTML document, rather than the top.
@JimPedid I like your response. I added a sentence on that as well. I hope, you don't mind?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.