jQuery: textarea default value disppear on click

前端 未结 8 1177
Happy的楠姐
Happy的楠姐 2021-02-04 16:40

I want a textarea with some default text. When the user clicks in the textarea the default text should be deleted. How can I make value of a textarea disappear on click?

<
相关标签:
8条回答
  • 2021-02-04 17:21

    Just try the below snippet. First time you click on the text area it empties the content.

    $('#textarea').focus(function(){
     $(this).empty();
    });
    
    0 讨论(0)
  • 2021-02-04 17:23

    You need two handlers, one for when the element gets focus and one for when it loses it. When it gets focus, check to see if the value is only space characters and, if so, set the value to the default.

    $('#textarea').focus( function(){
            var $this =$(this);
            if($this.val() == 'This should be removed..'){
                 $this.val('');
            }
    }).blur(function() {
            var $this = $(this);
            if ($this.val().match(/^\s*$/) { // matches only spaces or nothing
                 $this.val('This should be removed..');
            }
    });
    
    0 讨论(0)
  • 2021-02-04 17:25

    This should work:

    $('#txt')
        .focusin(function() {
            if ( this.value == 'Write something...' ) {
                this.value = '';    
            }
        })
        .focusout(function() {
            if ( this.value == '' ) {
                this.value = 'Write something...';    
            }
    });
    

    Live demo: http://jsfiddle.net/g7UKN/1/


    Update:

    This should do it:

    $('#txt')
        .each(function() {
            $(this).data('default', this.value);
        })
        .focusin(function() {
            if ( this.value == $(this).data('default') ) {
                this.value = '';    
            }
        })
        .focusout(function() {
            if ( this.value == '' ) {
                this.value = $(this).data('default');    
            }
    });
    

    Live demo: http://jsfiddle.net/g7UKN/2/

    0 讨论(0)
  • 2021-02-04 17:25

    Very simple non-jQuery-dependent solution:

    <textarea onfocus="if(this.value==this.defaultValue)this.value=''" onblur="if(this.value=='')this.value=this.defaultValue">Hello World</textarea>
    
    0 讨论(0)
  • 2021-02-04 17:35

    I use this as its a bit more generic - it will clear out the element's value on focus, but return the element's value to the default value if empty.

    $("#textarea")
      .focus(function() {
            if (this.value === this.defaultValue) {
                this.value = '';
            }
      })
      .blur(function() {
            if (this.value === '') {
                this.value = this.defaultValue;
            }
    });
    
    0 讨论(0)
  • 2021-02-04 17:35

    You can accomplish this even without JavaScript by using placeholder attribute.

    But you should be aware that not every browser supports it yet. In this case you can use for instance this plugin: http://plugins.jquery.com/project/input-placeholder

    0 讨论(0)
提交回复
热议问题