Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two trigger to one function ? [keyup and focusout]

Welcome,

I have function

 $('#myfield').keyup(function () {
//do something
}

//- do something is runing when user write something in myfield. I notice, when user use "auto complete" from browser, my function is not executed.

I found idea, to use focusout

Do you have any idea how can i combine that code together, without writing second function like this ?

 $('#myfield').focusout(function () {
//do something
}

I would like to put this 2 functions together, and don't write //do something, two times.

regards

like image 491
marc Avatar asked Nov 11 '10 12:11

marc


1 Answers

You can use .bind() which takes a space separated list of events to bind your handler to, like this:

$('#myfield').bind("keyup focusout", function () {
  //do something
});

Though, unless you need some special propagation, I'd stick with blur over focusout, just a preference really:

$('#myfield').bind("keyup blur", function () {
  //do something
});
like image 56
Nick Craver Avatar answered Sep 21 '22 10:09

Nick Craver