Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML : How to make a real "onchange" event in HTML (no jquery)?

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?

like image 948
jave.web Avatar asked Nov 19 '12 10:11

jave.web


People also ask

How do you call onChange in HTML?

The onchange attribute fires the moment when the value of the element is changed. Tip: This event is similar to the oninput event. The difference is that the oninput event occurs immediately after the value of an element has changed, while onchange occurs when the element loses focus.

How do you pass an event onChange?

To pass multiple parameters to onChange in React:Pass an arrow function to the onChange prop. The arrow function will get called with the event object. Call your handleChange function and pass it the event and the rest of the parameters.

Can a div have onChange?

No; the onchange attribute is only applicable to form field elements ( input , select , textarea , etc). Thanks for your answer. How do I do it?


2 Answers

Use onkeyup instead of onchange then.

like image 188
mabako Avatar answered Oct 16 '22 19:10

mabako


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.

like image 33
ameya rote Avatar answered Oct 16 '22 20:10

ameya rote