1

I have this code:

$('#email').keyup(function() {
            if(true || false)) {

            } else {

            }
        });

I need include this function also in blur event.

I've tried to create a jquery function but I could not. Somebody give me a light.

2

4 Answers 4

5

You can do this -

$('#email').on('keyup blur',function() {
Sign up to request clarification or add additional context in comments.

Comments

2

Use the on method to attach multiple events, which are specified in the first argument passed to the function.

$('#email').on('keyup blur', function() {
    if(true || false) {  //there was an extra ) here

    } else {

    }
});

Working Example http://jsfiddle.net/nv39M/

One thing to be aware of, the keyup event is going to fire prior to the blur event firing.

2 Comments

@user2465422 Glad I can help, let me know if you have any further questions. Good Luck!
I'm having trouble with the event change, don't work. when I click a value that firefox saves the form (one that has already been used), it does not perform validation. How to do it?
1

Make a separate function as follows

function funcName(){
//Your code
}

Now,use jQuery on

 $("#email").on("keyup",funcName);
 $("#email").on("blur",funcName);

For reference,check

http://api.jquery.com/on/

Comments

1

There are (at least) two ways you could achieve this.

  1. Specify multiple, space separated events as the first argument:

    $('#email').on('keyup blur',function() {
        // your logic
    });
    
  2. Use a named function:

    function yourFunction() {
        // your logic
    }
    
    $('#email').on('keyup', yourFunction);
    $('#email').on('blur', yourFunction);
    

Option 1 is probably the best choice assuming you don't want to use the function anywhere else, and that you want to bind the event handlers at the same time. If, however, you wanted to bind the blur event at a later point (perhaps in response to another event), or to a different element, then the named function method would be the best choice.

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.