asp.net textbox - client side textChanged event won't fire

Rodniko picture Rodniko · Nov 21, 2011 · Viewed 18.5k times · Source

i want my textbox to fire a textChanged client side (JS) event when a text is changed inside it.

i read many posts about it. most of them are talking about a code-behind event and the ones that talk about the client side tells to add attribute of onchange:

<asp:TextBox runat="server" ID="TextBox1" onchange="javascript: ontextchanged();"></asp:TextBox>

but this event only fires when i lose the focus on the textbox.

what is the solution to this ? how can i fire a JS function each time a text is changed inside the textbox?

Answer

Tim B James picture Tim B James · Nov 21, 2011

Use onkeyup or onkeydown instead.

This will then run the function when you type or click on the textbox. You can also then detect the keycode of the event, and prevent the function if you dont want it to run for certain keys.