Clear TEXTAREA value after clicking (onFocus)

I am currently using ..

onfocus="this.value=''; return false;"

to clear the value in my text box to leave comments. However, when you click to enter a comment, and enter a few things, if you were to click outside the field to possibly read something else on the page, and then return to your comment by clicking again in the textarea field, it will clear your current entry.

Is there a way to clear the text box on the first click to clear the default value and not the rest of the clicks for the rest of the browser page session?

+5
source share
9 answers

, , , . , textearea , , .

, , , , :

onfocus="if(this.value== "your initial text"){this.value=''}; return false;"
+8

, . , jQuery ( jQuery, ?), :

$(document).ready(function() {
    var _bCleared = false;
    $('form textarea').each(function()
    {
        var _oSelf = $(this);

        _oSelf.data('bCleared', false);
        _oSelf.click(function()
        {
            if (!_oSelf.data('bCleared'))
            {
                _oSelf.val('');
                _oSelf.data('bCleared', true);
            }
        });
    });
});

. onclick , textarea/textarea , Javascript html.

+3

function clearTA {
   if(this.value != "type here") {
      this.value = ''; 
      return false;
   }
}

html onfocus

...onfocus="clearTA()"..
+1
<script type="text/javascript">
function clearOnInitialFocus ( fieldName ) {
   var clearedOnce = false;
   document.getElementById( fieldName ).onfocus = (function () {
    if (clearedOnce == false) {
      this.value = '';
      clearedOnce = true;
    }
  })
}
window.onload = function() { clearOnInitialFocus('myfield');
</script>

: http://snipplr.com/view/2206/clear-form-field-on-first-focus/

+1

, , , , , -

onfocus="this.value=''; this.onfocus='';"

, , / .

+1

HTML5 placeholder jquery .

: http://jsfiddle.net/dream2unite/xq2Cx/

<input type="text" name="email" id="email" placeholder="name@email.com">

script:

$('input[placeholder], textarea[placeholder]').placeholder();

, () , ​​ , ().

, , jquery : http://mathiasbynens.be/demo/placeholder

+1

clearedOnce javascript, . . , true . . :

onfocus='if (!clearedOnce) { this.value = ''; clearedOnce = true;} return false;'
0

var.

In the item scriptabove the item

var tracker = {};

Further...

<textarea name="a" onfocus="if(!tracker[this.name]) { tracker[this.name]=true; this.value=''; } return false;">Placeholder text</textarea>
0
source

So, I suppose you have a pre-populated value, such as "Enter text here"? Try adding this javascript:

    function clearText(textarea,prefilled){
        if(textarea.value==prefilled)
            textarea.value="";
        return false;
    }

Then your entry should be: Enter your text here

This should help you get started ...

0
source

All Articles