Detect if the text field is empty or not, and run the code once, unless the state changes

I want to determine if this text field is empty or not, and then run some code on it.

Question: how can I run the code only once ... and then run it once every time the state of the text field changes for onefocus

  • Different states: emptyor not empty.

Further development:

(The most important part of these jsfiddle is shown on the console.)

  • Here is a jsfiddle of code that executes the code each (not what I want. Only the source code.).

    $('input').on("keyup", function() {
    
        var val = $(this).attr('value'),
            previous = $(this).prev();
    
        if ( val === '' ) {
            console.log('Empty');
        }
        else if ( val !== '' ) {
            console.log('Text');
        }
    
    });
    
  • And here is a jsfiddle of code that executes the code once per focus (this is somewhat close to what I want).

    , , , . ?

    $('input').on("focus", function() {
    
        $('input').one("keyup", function() {
    
            var val = $(this).attr('value'),
                previous = $(this).prev();
    
            if ( val === '' ) {
                console.log('Empty');
            }
            else if ( val !== '' ) {
                console.log('Text');
            }
    
        });
    
    });
    
+3
2

jQuery Text Change ZURB. , , , "hastext" "notext", :

http://www.zurb.com/playground/jquery-text-change-custom-event

.

( ).

LIVE DEMO:

http://jsfiddle.net/oscarj24/UtwNz/

, : -)

+1

All Articles