user2787300
user2787300

Reputation: 57

Uncaught TypeError: Cannot set property 'onclick' of null Not working after using window.onload

I am Getting the following error.

Uncaught TypeError: Cannot set property 'onclick' of null sample.aspx:40 sample.aspx 40
 - window.onload                                                         sample.aspx 40

I have search to solve this error But I am unable to successful. I found a lot of posting related to this error and every one are suggested to use Window.onload But still I could able to solve my issuse.

Here is my Code

<script>
         var b4 = document.getElementById('b4'),
          button4 = document.getElementById('set4');
         window.onload = function () {
             button4.onclick = function () {

                 // Update the Button
                 var pause = button4.innerHTML === 'start!';

                 return false;

             }
         };

    </script>

So any one please tell me how to solve this error.

Upvotes: 0

Views: 1968

Answers (1)

Karl-Andr&#233; Gagnon
Karl-Andr&#233; Gagnon

Reputation: 33880

You are setting you variable outside the onload. The DOM is not ready so the elements doesnt exist yet. Try putting it inside the load event :

     window.onload = function () {
         var b4 = document.getElementById('b4'),
          button4 = document.getElementById('set4');
         button4.onclick = function () {

             // Update the Button
             var pause = button4.innerHTML === 'start!';

             return false;

         }
     };

Upvotes: 1

Related Questions