4

With

<input type="text" onKeyPress="alert(this.value)">

the alert box displays the value in the box, not included the current key depressed.

However, this does not exhibit the same behavior, and I don't know why.

<input type="text" onKeyPress="handleInput(this.value)">

function handleInput(value){
    alert(value);
}

Added This http://jsfiddle.net/abalter/adbbb599/6/

5
  • Is that the order in the code? You defined the input first and then the function? Commented Mar 1, 2015 at 1:06
  • What is wrong with mine? jsfiddle.net/abalter/adbbb599/4 Commented Mar 1, 2015 at 1:10
  • Maybe use onkeyup instead? Commented Mar 1, 2015 at 1:13
  • @AustinMullins well, I load the js in the head. But, I just tried loading the js after the closing html tag, and now it works. I'm confused by that. Commented Mar 1, 2015 at 1:22
  • I think it has to do with how and when the browser resolves references to functions. It might be different across browsers. Apparently, the browser attempted to resolve the "onkeypress" handler before firing the "onload" event which defined the function. @hamism's answer would work with a script from a file. Commented Mar 1, 2015 at 1:30

3 Answers 3

6

First thing is avoid using js code inside html tags it is a bad practice , you can use the keyup event to get the content of the input after the user release any pressed key

<input id="input">

<script>
  var input = document.getElementById('input');
  input.addEventListener('keyup',function(){alert(input.value);});

</script>
Sign up to request clarification or add additional context in comments.

4 Comments

This works with it in a js file, but only if I load the js after the html. Why is that?
I want to ask about your comment regarding js code inside html tags. Would that be to stackoverflow computer science or web applicatoins?
Of course you have to wait for the document to load first then you can manipulate it, you can use window.onload = function(){here your code that is executed after the document is loaded} or using jquery with $(document).ready(function(){your code});
Putting js code inside html tags will slow your page loading time ,but if you use external js files you can then cache them and gain a considerable time i hope this is clear
2

It should exhibit the same behaviour. Make sure you load the javascript either in your head or body tags, so you can access the function from the html.

This code works for me:

<input type="text" onKeyPress="handleInput(this.value)">
    <script>
        function handleInput(value){
            alert(value);
        }
    </script>

4 Comments

In the live version, I load it in the head. In the jsfiddle, it's, well, just there.
In the jsfiddle, you can specify where it gets loaded up on the left. You have it to default, which is window.onload. This hides the function, but it would work if you did it the way hamism specifies above. You can also set wrap in <head> or wrap in <body> in jsfiddle.
Doing as you said works. Thanks. But I want to have the js in a .js file. hamism's method works in a js file, but only if I load the js after the html.
Doing it this way also works with a js file, you just have to put <script src="/path/file.js"></script>, but I agree with @hamism that it's better to avoid js code in your html tags.
1

Passing 'value' as a parameter is generally verbotim - its a reserved word. Your 'this' refers to the input field, and so 'this.value' returns the entire content of the input field. To get the keyCode from the keypress event try:

<body>
<input type="text" onKeyPress="handleInput();">
</body>
<script>
function handleInput(){
    alert(event.charCode);
}
</script>

This doesn't work with:

onKeyPress="event.charCode;"

or:

onKeyPress="this.event.charCode;"

because no event object is created.

Comments

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.