4

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.

bsr
  • 57,282
  • 86
  • 216
  • 316

5 Answers5

8

This should do it:

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

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

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
4

Part 1

You can just update it every keyUp, but I would suggest you at least wait 1 second after the user finishes typing.

var timer;

var changeTxt = function(){
    // Change label text here.
};

$("#myInput").keyup(function(){
    clearTimeout(timer);
    timer = setTimeout(changeTxt, 1000);
});

Part 2

That example you posted stops a form from submitting. Is that your goal?

EDIT:

I think you are trying to control the form's submission?

$("#myForm").submit(function(){
    if(/* Your condition here */){
        return false;
        //Only if your condition is true, stop form submission
    }
});
mattsven
  • 22,305
  • 11
  • 68
  • 104
  • thanks for ur answer..I will test it, Part 2: nope, I want the submit button to work, not the enter key. I saw a sol here http://stackoverflow.com/questions/1563062/jquery-prevent-form-submition-with-enter-key http://stackoverflow.com/questions/585396/how-to-prevent-enter-keypress-to-submit-a-web-form .. but wish an easy sol. – bsr Apr 24 '11 at 14:06
  • For that first one ( http://stackoverflow.com/questions/1563062/jquery-prevent-form-submition-with-enter-key ) you can use his function like: `$("#myForm").keypress(preventEnterSubmit);` – mattsven Apr 24 '11 at 14:10
0

there is something called oninput that you can use.

<form oninput="xx.value=aa.value">
   <input type="text" name="aa" value="">
   <output name="xx" for="aa"> </output>
</form>
NatureShade
  • 2,187
  • 1
  • 19
  • 27
0

Did you try out the keydown or keypress event?

Ivo
  • 3,406
  • 4
  • 33
  • 56
0

I would prefer a combination of both, form and field validation: Find working sample here: http://jsfiddle.net/ezmilhouse/9mNc4/1/

your html:

<form method="post" action="post.php">
    <input type="text" name="" value="" />
    <label>Name</label>
    <div></div>
</form>

your js:

// prevent form from being posted empty
$('form').live('submit', function(evt){
    if ( $('input', this).val() === "" ) {
        evt.preventDefault();
        alert('Field is required!');
    }
});


// validate form field on the fly
var min = 3;
$('input').live('keyup change', function(){
    if ($(this).val().length < min) {
        $('div').html('<span class="invalid">min. 3 characters.</span>');
    } else {
        $('div').html('<span class="valid">ok!</span>');
    }
});
ezmilhouse
  • 8,933
  • 7
  • 29
  • 38