Set default value in Textarea’s if empty

Asked

Viewed 109 times

1

I have two textarea on my page to give GET the value I have in my database and I want to delete the text of the two textarea automatically put a default value there "inside".

  • If it is something (only) visual you can use placeholder='valor padrão', without the need for Javascript.

1 answer

1


With Jquery you can do this in the event .change, this event is triggered when the value of an element is changed. In this event it can be checked whether the textarea is empty, if it is, we put a default value.

$('#text1').change(function(){
    if( $("#text1").val().length < 1){
        $('#text1').val('Valor padrão');
      }
});

DEMO

For versions prior to IE9 this can be done through the event onpropertychange.

$('#text1').bind('input propertychange', function() {
    if( $("#text1").val().length < 1){
        $('#text1').val('Valor padrão');
      }
});

DEMO

Browser other questions tagged

You are not signed in. Login or sign up in order to post.