[jquery] Clear text area

In Onselect event I have script:

$("#vinanghinguyen_images_bbocde").val('');
$("#vinanghinguyen_images_bbocde").val(vinanghinguyen_final_bbcode);

I want clear text area id="vinanghinguyen_images_bbocde" before add value to it. but textarea add add add add and value and not clear. I want clear it before add value

I use uploadify here is my function

<script type = "text/javascript" >
  $(document).ready(function() {
    vinanghinguyen_bbcode = '';
    vinanghinguyen_final_bbcode = '';
    vinanghinguyen_link = '';
    vinanghinguyen_final_derect_link = '';
    response = '';

    $('#file_upload').uploadify({
      'uploader'  : '{SITE_FULL_URL}/uploadify/uploadify.swf',
      'script'    : '{SITE_FULL_URL}/uploadify/uploadify.php',
      'cancelImg' : '{SITE_FULL_URL}/uploadify/cancel.png',
      'folder'    : 'data/picture_upload/2011',
      'auto'      : false,
      'multi'     : true,
      'buttonText': '',

      'onComplete': function(event, ID, fileObj, response, data) {
        vinanghinguyen_bbcode = '[IMG]' + 'http://cnttvnn.com' + response + '[/IMG]' + '\n';
        vinanghinguyen_final_bbcode = vinanghinguyen_final_bbcode + vinanghinguyen_bbcode;
        vinanghinguyen_derect_link = 'http://cnttvnn.com' + response + '\n';
        vinanghinguyen_final_derect_link = vinanghinguyen_final_derect_link + vinanghinguyen_derect_link;

        $("#vinanghinguyen_images_bbocde").val('').val(vinanghinguyen_final_bbcode);
      //$("#vinanghinguyen_images_derect_link").val(vinanghinguyen_final_derect_link);
        $("#vinanghinguyen_result").show();
        $(".uploadifyQueue").height(5);
      },

      'onSelect': function(event, ID, fileObj) {
        $("#vinanghinguyen_images_bbocde").val('');
        $("#vinanghinguyen_result").hide();
        $(".uploadifyQueue").height(315);
      },
    });
  });
</script>

This question is related to jquery uploadify

The answer is


Use $('textarea').val('').

The problem with using $('textarea').text('') , or $('textarea').html('') for that matter is that it will only erase what was in the original DOM sent by the server. If a user clears it and then enters new input, the clear button will no longer work. Using .val('') handles the user input case properly.


I agree with @Jakub Arnold's answer. The problem should be somewhere else. I could not figure out the problem but found a work around.

Wrap your concerned element with a parent element and cause its html to create a new element with the id you are concerned with. See below

<div id="theParent">
    <div id="vinanghinguyen_images_bbocde"></div>
</div>

'onSelect'    : function(event,ID,fileObj) {
 $("#theParent").html("<div id='vinanghinguyen_images_bbocde'></div>");
 $("#vinanghinguyen_result").hide();
 $(".uploadifyQueue").height(315);
}

When you do $("#vinanghinguyen_images_bbocde").val('');, it removes all the content of the textarea, so if that's not what is happening, the problem is probably somewhere else.

It might help if you post a little bit larger portion of your code, since the example you provided works.


Try this,

$('textarea#textarea_id').val(" ");

I just tried using this code and @psynnott's answer was correct though I needed to know it would work repeatedly, seems to work with jquery 1.7.1 >

I modified the jfiddle to the following http://jsfiddle.net/Rjj9v/109/

$('#mytext').text('');

This is not a new answer @psynnott is correct I am just providing a more concise example that shows the textarea is still working after the clear because if you use .val("") the text area stops working


I believe the problem is simply a spelling error when writing bbcode as bbocde:

$("#vinanghinguyen_images_bbocde").val('')

should be:

$("#vinanghinguyen_images_bbcode").val('')

Correct answer is: $("#selElement_Id option:selected").removeAttr("selected");


try this

 $("#vinanghinguyen_images_bbocde").attr("value", ""); 

This works:

$('#textareaName').val('');

Rather simpler method would be by using JavaScript method of innerHTML.

document.getElementById("#id_goes_here").innerHTML = "";

Rather simpler and more effective way.

   


This method removes not only child (and other descendant) elements, but also any text within the set of matched elements. This is because, according to the DOM specification, any string of text within an element is considered a child node of that element.

$('textarea').empty()