tags:

views:

41

answers:

1

I have an email sign up and search box that have the label inside the box, IE the email box says "sign up" inside the input. When you click in the box the value is removed allowing you to enter your email, if you enter nothing and click out, the "sign up" re appears. I am using the following code:

$(function() {
    swapValues = [];
    $(".swap_value").each(function(i){
        swapValues[i] = $(this).val();
        $(this).focus(function(){
            if ($(this).val() == swapValues[i]) {
                $(this).val("");
            }
        }).blur(function(){
            if ($.trim($(this).val()) == "") {
                $(this).val(swapValues[i]);
            }
        });
    });
});

However, I get the following error in console and am possibly having issues with other scripts due to this. Ill know for sure when I finish debugging.

error assignment to undeclared variable swapValues

Is there a better way to do this? thx

+1  A: 

I think you still need 2 Listeners

    $('.swap_value').focus(function(){
      if($(this).val() == 'sign up'){
        $(this).val('');
      }
    }).blur(function(){
      if($(this).val() == ''){
        $(this).val('sign up');
      }
    });

I hope thats gives you an example of how to work with 2 fields:

<html>  
    <body>   
        <input type="text" value="sign up" class="swap_value singup"/>
        <input type="text" value="search" class="swap_value search"/>
    </body>
 </html>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script>
   //<!--
    $(document).ready(function(){
        $('.swap_value').focus(function(){
          if($(this).val() == 'sign up' || $(this).val() == 'search'){
            $(this).val('');
          }
        });
        $('.search').blur(function(){
          if($(this).val() == ''){
            $(this).val('search');
          }
        });
        $('.singup').blur(function(){
          if($(this).val() == ''){
            $(this).val('sign up');
          }
        });
    });
    //-->
</script>
</body>
</html>
Hannes
@Hannes - this works great. Is there a way to use the same function for 2 boxes, if($(this).val() == 'sign up' || 'search') or do I have to write separate functions for each? when I do this it replaces 'search' with 'sing up' on blur
Dirty Bird Design
@Dirty Bird Design if they have the same class the listeners will apply to bove of them BUT while you can say `if($(this).val() == 'sign up' || $(this).val() == 'search')` ... ` $(this).val() == '' ` is always the same, so you need at least 3 listeners
Hannes
Yes, both are ".swap_value". I get where I can combine the first " if($(this).val() == 'sign up' || $(this).val == 'search') but not sure on the blur function, it keeps replacing the value with sign up how do you tie the search to the search listener?
Dirty Bird Design
@Dirty Bird Design Like i said,`$(this).val() == ''` will always be true for both input fields, so it will always apply, i will add something in my answer that may help you
Hannes
@Hannes - thx man, i learned something.
Dirty Bird Design