JQuery remove textarea placeholder when clearing textarea text

I have a Bootstrap modal with a text area. When you open a modal, the textbox should be cleared of any previous text to keep it fresh every time you open it.

The problem is that the placeholder in the field is also removed the first time the text is cleared.

I am using $('textarea').val('')

to clean up text.

http://jsfiddle.net/dkRS8/1/

+3


source to share


4 answers


You need to fire the event blur

for the placeholder to appear again. And it only works during the hide event, so when it reappears, the placeholder is returned.

$('#modal').on('hide', function() {
    $(this).find('textarea').val('').blur();
});

      



jsfiddle works

EDIT . I was wrong, it seems to work without blur, just changing the event from show

to hide

as explained by Martin in his answer where he uses the eventshown

+2


source


It works when you register for the "shown" event instead of the "show" event.



$(document).ready(function() {

    $('#modal').on('shown', function() {
        $(this).find('textarea').val('');
    });

});

      

+2


source


try

$(document).ready(function() {
    $('#modal').bind('hide',function(){
        $('textarea',this).val('')
    })
});

      

multi-browser, jquery-ui event standar

0


source


Among IE versions .. IE 10 only supports placement holder .... So don't try to use this tiger in older IE versions ...

but other browsers like firefox, Chrome, Opera ... support this attribute ...

try the below code, it will help you ...

Updated script: http://jsfiddle.net/dkRS8/6/

$(document).ready(function() {

    $('#modal').on('show', function() {
        var ele = $(this).find('textarea');
        ele.val('');
    });

});

      

-1


source







All Articles