How to create a function for this code?

How to create a function for this code?

Hi,

My code looks like this:

  1.      var firstInput = $('#user-login-form :input:enabled:visible:first');
  2.      firstInput.focus();
  3.      firstInput.addClass('dirty');
  4.      firstInput.prev('label').addClass('dirty');

  5.      $('.js-signInButton').click(function(e){
  6.         e.preventDefault();
  7.         $('#edit-email').focus();
  8.         var firstInput = $('#user-login-form :input:enabled:visible:first'); 
  9.         firstInput.focus();
  10.         firstInput.addClass('dirty');
  11.         firstInput.prev('label').addClass('dirty');
  12.       });

  13.      $('.js-registerButton').click(function(e){
  14.         e.preventDefault();
  15.         $('#edit-firstname').focus();
  16.         var firstInput = $('#user-register-form :input:enabled:visible:first');
  17.         firstInput.focus();
  18.         firstInput.addClass('dirty');
  19.         firstInput.prev('label').addClass('dirty');
  20.       });

However as you can see the part which put the focus on the first input is repeating almost 3 times with the difference that var firstInput has different values.

The code is working but it doesn't look nice and clean. How can I make a function from the part which put the focus on the first input and call it whenever I need it?

Thanks