Why the javascript program is returing null?It should return objectHTMLunknwonelement
<html> <head> </head> <body> <script type="text/javascript"> function mySignature() { document.write("samir imtiaz<br/>"); document.write("www.fb.com/samir.imtiaz37"); var k=document.getElementById("para2"); alert(k); } <script> mySignature(); </script> <h2> it is head 2</h2> <p id="para1" onmousemove="effect()" onmouseout="effectback()"> this is my first paragraph </p> <p1 id="para2">it is paragraph </p1> <button onclick="mySignature()"> my sign </button> </body> </html> Expected output while tapping "my sign" button :objectHTMLunknwonelement
Output that i observed:null
<html> <head> </head> <body> <script type="text/javascript"> function mySignature() { var k=document.getElementById("para2"); alert(k); } <script> mySignature(); </script> <h2> it is head 2</h2> <p id="para1" onmousemove="effect()" onmouseout="effectback()"> this is my first paragraph </p> <p1 id="para2">it is paragraph </p1> <button onclick="mySignature()"> my sign </button> </body> </html> Expected output while tapping "my sign" button :objectHTMLunknwonelement
Output that i observed:objectHTMlunknownelement
#para2is defined in thedocument. You can useloadevent ofwindow<script> onload = mySignature; </script>It should return objectHTMLunknwonelementno. It will always returnnullif the element does not exist - developer.mozilla.org/en-US/docs/Web/API/Document/…effectandeffectbackfunctions are not defined at the code at Question