• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

jQuery question. Binding multiple key actions to single function

Hi Guys, I have the following function for the jquery 'focusout' action. I'd like to bind both focusout AND keyUp to this function.

Is there a way to do this without basically writing each function twice?

Code:
$('#registerFirstName').focusout(function(){
  var checkregisterFirstName=$(this).val();
  if(checkregisterFirstName == ''){
    $registerFirstName.addClass("error");
    $('#registerFirstNameBlankError').show();
  }
			
  else {
    $registerFirstName.removeClass("error");
    $('#registerFirstNameBlankError').hide(); 
  }

});
 
Well, function(){} returns a function reference. So you could save that in a variable and pass that variable to focusout() and keyUp().

Or name your function ("function myname(){}") outside this function, and then pass the function name to focusout(myname) and keyUp(myname).
 
I am not jQuery expert, but it looks like you are just declaring a function withing the .focusout. Couldn't you declare that function somewhere else, name it, and just reference it in the .focusout() and .keyUp() function?

Edit: my reading comprehension is lacking today because I basically said exactly what Ken_g6 said. =(
 
Last edited:
Code:
$('#registerFirstName').on('focusout keyUp', function(){
  var checkregisterFirstName=$(this).val();
  if(checkregisterFirstName == ''){
    $registerFirstName.addClass("error");
    $('#registerFirstNameBlankError').show();
  }
			
  else {
    $registerFirstName.removeClass("error");
    $('#registerFirstNameBlankError').hide(); 
  }

});
 
Back
Top