2

I am learning Javascript.. Below code working only if my script tag below my input text element, but if i place code above input text tag, its not working. May I know why? Below is code:

<head>    
</head>
<body>
    <input type="text" id="name" >   
    <script type="text/javascript">             
        var txtId = document.getElementById('name');    
        txtId.addEventListener('keypress', function(e){
            console.log('Pressed!')
        })                      
    </script> 
</body>

Below code is same as above except that I am using function, inside which I am using same code as above. But in this case, my script tag is above input text tag, and its working. How it's working in this case? Below is the code:

<head>
    <script type="text/javascript">   
        function keyPressListener(){
            var txtId = document.getElementById('name');   
            txtId.addEventListener('keypress', function(e){
                console.log('Pressed!')
            })
        }                   
    </script>
</head>
<body>
<input type="text" id="name" onkeypress="keyPressListener()">

</body>

So, what exactly difference between above 2 codes?

3 Answers 3

2

When you are using the onkeypress attribute. It actually works the same way as the addEventListener. You just have to write a simple function and call it in onkeypress

<input type="text" id="name" onkeypress="keyPressed()">

<script>
function keyPressed(){
    console.log('Key Pressed');
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

2

Why is not working to place above the input -Because document was not ready .so you need body.onload event .see the body onload=start() it will apply the js function after body loaded

<body onload="start()">
  <input type="text" id="name">

  <script type="text/javascript">
    function start() {
      var txtId = document.getElementById('name');

      txtId.addEventListener('keypress', function(e) {
        console.log('Pressed!')
      })
    }
  </script>

</body>

And the second one -you are using two function in a single event. So use with any one of the event

if use with inline keypress of keyPressListener() else use Dom event of the keypress (addEventListener)

*Note:

  1. Dont include the addEventListener() inside keyPressListener() .
  2. If you use with addEventListener() remove the onkeypress event inline of the markup.
  3. because both are same action .

<head>
<script type="text/javascript">
  function keyPressListener() {
      console.log('Pressed!')
  }
</script>
</head>

<body>
  <input type="text" id="name" onkeypress="keyPressListener()">

</body>

Comments

-1

You can use addEventListener.

Or try this as your input:

Add a ; after keyPressListener():

<input type="text" id="name" onkeypress="keyPressListener();">

If that doesn't work try this:

<input type="text" id="name" onkeypress="keyPressListener(); return true;">

HTML5 knows that when you use an onkeypress attribute that it needs to call the JavaScript function when the key is pressed. You can basically put any functional JavaScript in the parameter for the onkeypress="(JavaScript goes here)" attribute.

You can also just grab the element from the DOM and then add the event listener to the element like so:

jQuery: $('#name').onkeypress( function() { //code goes here } );

Regular Js: document.getElementById('name').onkeypress( function() { //code goes here } );

1 Comment

Why would you say that "addEventListener is deprecated"? developer.mozilla.org/en-US/docs/Web/API/EventTarget/… certainly seems to disagree.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.