Scope question and basic understanding : access jquery from within function?

Scope question and basic understanding : access jquery from within function?

Alright, I'm new to jQuery. I am still trying to get the hang of it...

The problem I have:

I have 2 textfields on a page (tbUsername and tbPassword), and want to manipulate them. When a user clicks or focusses on tbPassword i need to do the same. So instead of this:



  1. $('tbPassword').click(function) () {
  2.   //do same code as onFocus
  3. });
  4. $('tbPassword').focus(function) () {
  5.   // do same code as onClick
  6. }
I do this:
 
  1. $('tbPassword').click(function) () {
  2.   handle_tbPassword_events(this);
  3. });
  4.  
  5. $('tbPassword').click(function) () {
  6.   handle_tbPassword_events(this);
  7. });
  8. function hande_tbPassword_events(obj) {
  9.   // but, from this function i need to access tbUsername..
  10.   // $('tbUsername').value = 'something'; does not work, cannot access jQuery (i think)
  11. }
So, what is the proper way to code this?
  • Should I create a global variable for tbUsername?
  • Should I create a global instance of '$'? (seems very wrong to me)
  • Can I combine 2 events in one expression? Something like:
    $('tbPassword').click(function).focus(function) {
      //do something
    }
    And in that case, how do I access the 2 events?



  • Or should I somehow "plug" my function to jQuery? Like extending jQuery?

Some help and/or suggestions are appreciated very much ..