I would like to trigger a function when my user blurs or enters a text box. I wrote this code to implement that:
text:function(input,el){
var textCheck = function(){
var value = $.trim(el.val());
if(!value){
el.addClass('error');
}else{
el.removeClass('error');
}
}
$(el).on('blur click keyup', textCheck); //calling the function
$(el).on('keyup', function(e){ ////calling the function again
if(e.keyCode == 13){
textCheck();
}
});
//instead of calling 2 times, how can i put together as single call?
}
Can I shorten the code that binds the events?
Yes! You can use
.bind()jQuery method which excepts the map of event handlers: