0
var obj1 = document.getElementById("p");
var obj2 = document.getElementById("btn");

alert(obj1);
alert(obj1);

This Code in JavaScript is returning null. I have both id 'p' and 'btn' in my html as Lable and Button respectively.

I have tried all other ways in my knowledge to solve this problem like

document.getElementById('btn').addEventListener('click', function () {
            alert('Click..!!');
});


document.getElementById('btn').onclick = function () {
    alert('Click..!!');
}; 

and others including Jquery but can someone point why it is returing null instead of HTMLObject? Thanks.

UPDATE html as:

<body>
    <label id="p">L</label>
    <button id="btn" value="Click" >Click</button>
</body>
DotNET
  • 55
  • 8
  • 1
    Add the relevant HTML to your question. – Andy Feb 12 '16 at 12:35
  • HTML Added in update – DotNET Feb 12 '16 at 12:38
  • `` and `` – Jimish Gamit Feb 12 '16 at 12:40
  • This in-fact is a simple problem. I had this knowledge that I shall put the script at the place in HTML so that it should not be called before the button itself is rendered, which is a problem might most beginners face like me. And thanks tushar for linking with very clear and concise discussion and others.. – DotNET Feb 12 '16 at 19:25

0 Answers0