62

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>
vinanghinguyen
  • 1,095
  • 4
  • 10
  • 17
  • 1
    Which version are you using? It works fine for me. http://jsfiddle.net/MD33Z/ – mmhan Nov 27 '11 at 10:45
  • Hi,select an answer from the 9 answers you have so far, or post an answer if you found the solution so we can benefit from your question. Thank you. – samouray Nov 05 '15 at 15:21

11 Answers11

86

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.

Jakub Arnold
  • 79,807
  • 86
  • 218
  • 314
48

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.

Karl Wenzel
  • 2,150
  • 23
  • 23
20

This works:

$('#textareaName').val('');
Undo
  • 25,204
  • 37
  • 102
  • 124
mzonerz
  • 1,070
  • 11
  • 19
6

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()
6

try this

 $("#vinanghinguyen_images_bbocde").attr("value", ""); 
confucius
  • 12,529
  • 10
  • 45
  • 66
3

Try this,

$('textarea#textarea_id').val(" ");
Achu S
  • 137
  • 9
2

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

duindain
  • 475
  • 3
  • 12
  • Thanks for negging my answer even though its the only one with a working fiddle and it is indeed still correct. Perhaps you should try going to the fiddle and swapping text to val and watch it break after the first change before incorrectly negging answers – duindain Feb 14 '17 at 21:20
  • I didn't neg it. I don't know who did. I was just giving an explanation as to why someone might have. – Kristen Hammack Feb 14 '17 at 21:56
  • Fair enough, did you try the fiddle yet? since it does break my answer is still correct – duindain Feb 15 '17 at 10:31
  • That's interesting. Using .val('') seems to break your fiddle, but it doesn't make the text area "stop working". If you type in the text area, it works, and you can get the value from it for a form. Apparently you just can't append to it. – Kristen Hammack Feb 15 '17 at 13:03
1

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

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

Rather simpler and more effective way.

   

Phani Kumar M
  • 4,405
  • 1
  • 11
  • 25
  • 1
    Actually it's `document.getElementById("id_goes_here").innerHTML = "";` # is a jQuery selector. But good job on posting the native JS version. – SystemFailure Jul 27 '18 at 17:59
1

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

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

should be:

$("#vinanghinguyen_images_bbcode").val('')
Dharman
  • 21,838
  • 18
  • 57
  • 107
Daniel Nordh
  • 363
  • 2
  • 15
0

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

user1920925
  • 491
  • 5
  • 4
0

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);
}
Neo
  • 712
  • 1
  • 7
  • 21