1

All I want is to display a success message when a user changes the input value. If it starts off as foo and a user types s making foos, I want to display a success message.

What am I missing?

Simple JSFiddle: http://jsfiddle.net/DdyNN/

HTML:

<input type="text" name="foo" id="foo" value="foo">
<div id="bar"></div>​

JS:

$('#foo').change(function() { 
    $('#bar').html('yay!');
});​
1
  • it works jsfiddle.net/DdyNN/2 If you need "live"-changes - use keyup event Commented Nov 28, 2012 at 10:07

5 Answers 5

3

the change event triggers when you leave the textfield. I would register to the keyup or keydown event.

Links:

Sign up to request clarification or add additional context in comments.

Comments

1

This works:

$('#foo').keyup(function() {
    if($(this).val()=='foos'){
        $('#bar').html('yay!');
    }
});​

Comments

1

$.change() is for selects and not for input types. Try:

var currentvalue = $('#foo').val();
$('#foo').focusout(function(){
if($(this).val() != currentvalue){
$('#bar').html('yay!'); currentvalue = $(this).val();
}
});

Comments

1

Try this,

Live Demo

prv = $('#foo').val();
$('#foo').keyup(function() {
    if(prv + "s" == $(this).val()) 
       $('#bar').html('yay! success');
    else
       $('#bar').html('yay! failure'); 

        prv = $('#foo').val();
});​

Comments

1

This work on key pressed on the textbox when is focus

$('#foo').focus(function() { 
   $('#foo').keyup(function(){
     if($(this).val()=='foos')$('#bar').html('yay!');
      });
});​

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.