views:

78

answers:

3

How to save the value of INPUT in variable to not to write a lot of duplicate code?

like var input = $(this).val();

full example

<div id="form">
    1. <input type="text" value="title" />
    2. <input type="text" value="value" />
</div>

$(function(){
  $('#form input:eq(0)').bind({
    focus: function(){
       if($(this).val()=='title'){
            $(this).val('');
        }
     },
     blur: function(){
       if($(this).val() == ''){
          $(this).val('title');
        }
     }
  });

  $('#form input:eq(1)').bind({
    focus: function(){
      if($(this).val()=='value'){
         $(this).val('');
      }
    },
     blur: function(){
        if($(this).val() == ''){
           $(this).val('value');
        }
    }
  });
});
+3  A: 

I'm not exactly sure what you are asking, but this refactoring will work for toggling the value. EDIT: added default attribute to the html elements and shortened jQuery (still readable though) http://jsfiddle.net/UmZeZ/

<div id="form">
    1. <input type="text" value="title" default="title" />
    2. <input type="text" value="value" default="value" />
</div>


$(function() {
    $('#form input').bind('focus blur', function() {
        var value = $(this).attr('default');
        if ($(this).attr('value') == value) {
            $(this).attr('value', '');
        } else if ($(this).attr('value') === '') {
            $(this).attr('value', value);
        }
    });
});
Scott
I refactored again, this time editing the input elements and combining the bindings. I think this is as short as you're going to get while still being readable.
Scott
+2  A: 

Here is my solution. I would work to any field which has class="set-default"

Checkout the working example

Here is the code:

$(function(){
    $('.set-default').bind({
    focus: function(){
        if(typeof($(this).data('def')) == 'undefined'){
               $(this).data('def', this.value)
        }
        if(this.value == $(this).data('def')){
           this.value = '';
        }
     },
     blur: function(){
       if(this.value == ''){
          this.value = $(this).data('def');
       }
     }
    })
});

basically all fields which had the class set-default will act as you like. You can always change the selector to $('#form input') but I think it's not useful.

HTH

Nik
Thanks, almost what I wanted
Algorithm
Don't compare against `undefined` - use `typeof`, don't create custom attributes - use `.data()`, don't wrap *everything* in jQuery's `$()` when you can use the DOM instead.
Yi Jiang
@Yi Jiang - totally right! re factored the code according to the best practives :)
Nik
+3  A: 

To accomplish what you want, I would suggest using the HTML5 placeholder attribute. With Modernizr, we can detect browser support for this feature, and with this simple piece of code, we can get it to work even for browsers that do not support placeholder.

if(!Modernizr.input.placeholder){
    var input = $('input[type="text"]');

    input.focus(function(){
        if(this.value === this.getAttribute('placeHolder')) this.value = '';
    }).blur(function(){
        if(this.value === '') this.value = this.getAttribute('placeHolder');
    }).blur();
}

See a live demo of this here: http://www.jsfiddle.net/yijiang/cTDsL/1

Yi Jiang
your code work only if user doesn't go again in that field. If he return the field get blank again, but this is not useful. Also why you need to rely on HTML5 while jQuery (as you said) provide .data() function. If the user work with browser without HTML5 support your solution wont work.
Nik
@Nik Right, fixed. This works no matter whether HTML5 is used and supported or not. There's no reason why HTML5 shouldn't be used as soon as the browsers support them.
Yi Jiang