7

No much to say,

<html>
    <body>
        <script>
            var x = document.getElementById('btn1');
            alert(x);
        </script>
            <input type="button" id="btn1" value="Clickme" onclick="alert('1')" />
    </body>
</html>

The alert message is null instead of a message contain details of the object or something like that.

What is the problem?

Billie
  • 8,100
  • 12
  • 31
  • 62

7 Answers7

31

Put your script AFTER the actual element, otherwise by the time your javascript code runs, there's not yet such element in the DOM.

<html>
    <body>
        <input type="button" id="btn1" value="Clickme" onclick="alert('1')" />
        <script>
            var x = document.getElementById('btn1');
            alert(x);
        </script>
    </body>
</html>

Alternatively put your script in a DOM ready event to ensure that the DOM is fully loaded by the browser before attempting to manipulate it:

<html>
    <body>
        <script>
            window.onload = function() {
                var x = document.getElementById('btn1');
                alert(x);
            };
        </script>
        <input type="button" id="btn1" value="Clickme" onclick="alert('1')" />
    </body>
</html>
Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
7

You need to run the javascript after the html s read/rendered...

Try this instead:

<html>
    <body>
        <input type="button" id="btn1" value="Clickme" onclick="alert('1')" />
        <script>
            var x = document.getElementById('btn1');
            alert(x);
        </script>
    </body>
</html>

Or you can add a window load function so the script only runs after the page has loaded. Like:

<html>
    <body>
        <script>
            window.onload = function(){
                var x = document.getElementById('btn1');
                alert(x);
            }
        </script>
            <input type="button" id="btn1" value="Clickme" onclick="alert('1')" />
    </body>
</html>
Sergio
  • 27,160
  • 10
  • 79
  • 126
0

Its because your are trying to access the button information before its created in the dom of the document.Give the script a the end of the body like below

<html>
    <body>

        <input type="button" id="btn1" value="Clickme" onclick="alert('1')" />
        <script>
            var x = document.getElementById('btn1');
            alert(x);
        </script>
    </body>
</html>

DEMO

Kawinesh SK
  • 2,998
  • 1
  • 13
  • 29
0

Javascript codes run from top to bottom. In the line alert(x), there is no input#btn1 yet. You can use:

window.onload = function() {
    var x = document.getElementById('btn1');
    alert(x);
}
0
<script type="text/javascript">
     function call()
     {
        var x = document.getElementById('btn1').value.trim();//or use "=<%btn1.ClientID%>"
        alert(x);
     }
      </script>

put this function onClientClick event of your button.

The Hungry Dictator
  • 3,247
  • 4
  • 34
  • 47
0

When the script is executed first there is no element with id #btn1 loaded in the dom. So execute the script after the element is loaded.

Like this,

    <input type="button" id="btn1" value="Clickme" onclick="alert('1')" />
    <script>
        var x = document.getElementById('btn1');
        alert(x);
    </script>
</body>

Prasanth K C
  • 6,307
  • 5
  • 33
  • 58
0
window.addEventListener('DOMContentReady',function(){
    var x = document.getElementById('btn1');
    alert(x);
};

Using 'DOMContentReady' event is better than 'window.onload' if your broswer support.

yoyo
  • 712
  • 6
  • 4