<!doctype html> <script> let i; window.onload = init; function init (){ i = document.getElementById("txt"); alert(i); } </script> <body> <p1 id="txt">Something</p1> </body>