19
$(".box_yazi2").each(function () {
    var default_value = this.value;
    $(this).css('color', '#555'); // this could be in the style sheet instead
    $(this).focus(function () {
        if (this.value == default_value) {
            this.value = '';
            $(this).css('color', '#000');
        }
    });
    $(this).blur(function () {
        if (this.value == '') {
            $(this).css('color', '#555');
            this.value = default_value;
        }
    });
});

This function of default value of input doesnt work in FF, but perfectly works in IE and ofcourse the input itself looks like this:

<input type="text" class="box_yazi2" id="konu" name="konu" value="Boş" />
5
  • 6
    Why don't you use this.defaultValue? Commented Jan 4, 2011 at 8:54
  • 2
    The last line is an extra set of });. Is this just incorrectly copy-pasted? Commented Jan 4, 2011 at 8:57
  • @venom Well, it works in Fx here. Commented Jan 4, 2011 at 8:58
  • does Firebug return an error of some kind maybe? Also, if you just want to get this done (and don't do it for learning purpose): mucur.name/system/jquery_example Commented Jan 4, 2011 at 8:59
  • @Box9 yeah ) it is the last function in document.ready )) @jensgram I will try it thx Commented Jan 4, 2011 at 9:05

6 Answers 6

81

Just use the defaultValue property:

var default_value = $(this).prop("defaultValue");

Or:

var default_value = this.defaultValue;
Sign up to request clarification or add additional context in comments.

7 Comments

you should elaborate your example. currently there's no attribute defaultValue
defaultValue is a built-in attribute accessible via JavaScript. For input[type=text], it contains the original value of the input element... the value that was present when the page was loaded.
NICE! I had no idea. Is this attribute widely supported/accessible in this manner?
@crazy: See Interface HTMLInputElement as described in DOM level 2 specs and HTMLInputElement on MDN. Other interesting attributes are defaultChecked and defaultSelected for radio/checkboxes and options inside select element.
There's a diff between attributes and properties in HTML. for getting the defaultValue or defaultChecked, we should use the .prop() method. There's a good explanation at jQuery.com: api.jquery.com/prop
|
9

The solution is quite easy; you have an extra }); in your code (thanks @ Box9).

I would encourage you to reuse the variable and not create dozens of jQuery objects.

I've changed your example to background-color but it will work.

$('.box_yazi2').each(function(index, element) {
    var $element = $(element);
    var defaultValue = $element.val();
    $element.css('background-color', '#555555');
    $element.focus(function() {
        var actualValue = $element.val();
        if (actualValue == defaultValue) {
            $element.val('');
            $element.css('background-color', '#3399FF');
        }
    });
    $element.blur(function() {
        var actualValue = $element.val();
        if (!actualValue) {
            $element.val(defaultValue);
            $element.css('background-color', '#555555');
        }
    });
});

demo

Comments

2
$('input[type="text"]').focus( function(){
            elementValue = $(this).val();
            $(this).val("");
        });
        $('input[type="text"]').blur( function(){
            if($(this).val() != elementValue && $(this).val() != ""){

            }else{
                $(this).val(elementValue);
            }

        });

Comments

1

I'm using the next code:

    //clear the focused inputs
$('input[type="text"]').focus( function(){
    if( $(this).attr('value') == $(this).attr('defaultValue') ){
        $(this).attr('value', '');
    };
} );
$('input[type="text"]').blur( function(){
    if( $(this).attr('value') == '' ){
        $(this).attr('value', $(this).attr('defaultValue') );
    };
} );

1 Comment

You shouldn't use .attr('value', ...) but .val(...) to change an element's value!
1

Use this.defaultValue

Sorry for the link to w3notcools, http://www.w3schools.com/jsref/prop_text_defaultvalue.asp

3 Comments

w3schools is a horrible resource. A better reference would be developer.mozilla.org/en-US/docs/DOM/…
ThiefMaster, I learned a lot thanks to w3schools and I can't share your view of it being 'horrible resource'. Just because there are better and more detailed or more advanced resources than mentioned website, it doesn't make one 'horrible'. Also, the fact that they don't have anything with the W3C or that someone created a website to bash them - doesn't mean anything regarding their 'horribleness'. To tell the truth - W3C themselves had so much awful solutions/decisions that they are the last to bash other people/companies.
0

You should use prop instead of so many functions to be honest, use 'delegate' instead of 'on' for late static binding.

$('.box_yazi2').each(function() {

$(this).on('focus', function(){

   if($(this).val() == $(this).prop('defaultValue')){

      $(this).val('');
      $(this).css('color', '#000');
    }

});

$(this).on('blur', function(){

   if($(this).val() == ''){

      $(this).val($(this).prop('defaultValue'));
      $(this).css('color', '#000');
   }

});

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.