6

Please note: I do not want to use jQuery for this (otherwise I like it)

Problem: I have come to situation where I need to do some javascript function after an user changes an input field (e.g. input type="text"). So I said to myself - I remember an onchange event - BUT onchange event runs AFTER user leaves the field, but I need the function to be called for example every time user types a character to that field. Wowhead shows nice example of what I am trying to achieve this (field with placeholder "Search within results...")

Summary: I am looking for a SIMPLE way of detecting REAL onchange event(not the classic HTML one)and through that call a JS function while not using jQuery?

3
  • 1
    You should listen to onkeyup events. Commented Nov 19, 2012 at 10:42
  • 1
    Be careful of just listening for keyboard events. Remember that a user can cut from and paste to the field using a pointing device. Commented Nov 19, 2012 at 13:02
  • @Alohci well thats a really good point! But fortunatelly, users have almost no way to copy it in my app :) and the whole mouse-listening thing is the whole issue itself :) Commented Apr 30, 2013 at 7:34

4 Answers 4

10

Use onkeyup instead of onchange then.

Sign up to request clarification or add additional context in comments.

1 Comment

YEAH ! That was EXACTLY what I was looking for! Thanks a lot! BTW: I knew this event but I really didnt expect it to act like that in a text field :-O Thanks a lot again :)
4

Following is a simple way of invoking each type of Key Press on field.

input type="text" onkeypress="myFunction()

Get Example here

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onkeypress

Enjoy..

you can also use onkeyup and onkeydown instead of onkeypress.

Comments

1

Tried onkeypress="myFunction()" or onkeyup="myFunction()"?

There are also events for onfocus and onblur for entering and leaving a textfield :)

Comments

0

You should use "input" event. keyup and keypress don't work if a user modified the value only by a mouse.

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.