javascript - what does this.id return? -


function test(){ alert(this);//alerts oblect alert(this.id);//alerts undefined  }  <input type="text" onkeypress="test();" id="text1"> 

why alert(this.id) alert undefined? because this returns document object?

thank you

your code should be.

function test(objref){   alert(objref);//alerts oblect   alert(objref.id);//alerts undefined }  <input type="text" onkeypress="test(this);" id="txtnum" /> 

edit: may use following code too.

<script type="text/javascript">         window.onload = function() {             var txtnum = document.getelementbyid("txtnum");             txtnum.onkeypress = function() {                 alert(this);                 alert(this.id);             };         };  </script>   <input type="text" id="txtnum" /> 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -