Javascript - Can I run my event handler AFTER the default handler for the "onkeypress" event? -


i have input text , want run function after default event handler

<input type="text" onkeypress="myfunction()"></input> 

the function

function myfunction(){alert($("input")[0].value)} 

because myfunction() using input value , property changed after default handler runs , change it. example: if text field has value "1234" , , pressed key "5", function alert "1234" not "12345"

so, want default function runs first change value property , can use in myfunction()

can ?!

 onkeypress="default();myfunction()" 

i did work around putting myfunction() in onkeyup event, don't want that.

thanks, , please consider i'm newbie.

trivial solution:

you should use oninput instead of onkeypress. so:

<input type="text" oninput="myfunction(event)" />

the keypress event emitted when key gets pressed, i.e., before dom modified pressed key's value. why never got value of last key pressed.

the input event emitted when data entered input element. can access data reading value of element.


using event-binding:

  1. html

<input id="input_field" type="text" />

  1. jquery
 $(document).ready(function() {     $("#input_field").on("input", function() {     console.log($(this).val());   }); }); 
  1. pure js
     var input_field = document.getelementbyid("input_field");     input_field.addeventlistener("input", function(event) {         console.log(event.target.value);     }); 

note: add in script tag after html or use window.onload simulate behaviour of $(document).ready.

see so answer more information how event-binding works.