TunaFFish
TunaFFish

Reputation: 11302

jquery combine event functions

instead of example:

$(".myfield").blur(function() {
    // validate
})
$(".myfield").keyup(function() {
    // validate
})

is there a way to combine these two?

Upvotes: 20

Views: 13065

Answers (3)

Yasir Aslam
Yasir Aslam

Reputation: 507

use with .on() Event

$(document).on("keyup blur", "#myfield", function(event)
    {       
    // your code        

    });

Upvotes: 4

gearsdigital
gearsdigital

Reputation: 14205

In case you want to validate each for itself...

$('.myfield').live('blur keyup', function(event) {
  if (event.type == 'blur') {
    // validate on blur
  }
  if (event.type == 'keyup') {
    // validate on keyup
  }
});

Upvotes: 6

jAndy
jAndy

Reputation: 235962

Yes

$(".myfield").bind('blur keyup', function(){
  // validate
});

Reference: .bind()

Upvotes: 42

Related Questions