Drupal jQuery input value function

Drupal jQuery input value function

Hi, I can't seem to get his code to work which I took from:
http://webdevmania.com/archive/top_10_jquery_snippets_including_jquery_1.4/

I want it so that when a user click on a textfield (classname "form-text") the value should disappear and reappear when clicking outside of it. Just a user-friendly function to put it simple.

  1. <?php
  2. drupal_add_js(
  3. 'swap_val = []; 
  4.  $(".form-text").each(function(i){ 
  5.      swap_val[i] = $(this).val(); 
  6.     $(this).focusin(function(){ 
  7.          if ($(this).val() == swap_val[i]) { 
  8.              $(this).val(""); 
  9.          } 
  10.    }).focusout(function(){ 
  11.         if ($.trim($(this).val()) == "") { 
  12.           jQuery(this).val(swap_val[i]); 
  13.      } 
  14.     }); 
  15.  });',
  16. 'inline'
  17. );
  18. ?>
If you are familiar with Drupal you probably know that you have to add <?php tags and the drupal_add_js-function stated here: http://api.drupal.org/api/function/drupal_add_js/7

But I can't get it to work!? In Drupal I have activated php-code in my textareas, so I am allowed to use that. I'm not getting any errors so I guess I'm doing something right. However, I do get errors if I really want to, just to try if things are really working.

Any guesses what could be wrong, or missing in my code? And yes I am certain that the class-name for the input fields are "form-text".

Edit: Just wanted to say that I got it to work with puting document ready-function :P