Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: event for simulating live typing

Part 1:
Is there any event I can use to get a callback when the user 'change' the input field. My definition of change is to simulate the following effect. say, I want to update a label while the user typing in the input box. I tried jquery "change" event. It works, but doesn't have the live effect. Once the input field is updated, I have to click on somewhere in the screen to update the label.

Part 2:

well, if this is not a good idea, I may prevent the form being submitted on enter key. Not sure about a good way to do it either. Quick search found this answer.

<form action="" method="post" onsubmit="return false;">

not tested yet, but hopefully the submit button may still works.

EDIT: tested, and onsubmit="return false;" prevents even the submit button.

thanks,
bsr.

like image 863
bsr Avatar asked Dec 16 '22 14:12

bsr


1 Answers

This should do it:

input.bind('keydown keypress', function() {
    setTimeout(function() {
        label.text(input.val());
    }, 0);
});

Live demo: http://jsfiddle.net/simevidas/qTBxv/

like image 160
Šime Vidas Avatar answered Dec 24 '22 15:12

Šime Vidas