1

I am trying to replace the content that pastes in the Textarea, but facing a problem that it shows in the Textarea the original content and the "modified" content both. Hope you can help me.

HTML Code (just a simple textarea):

<textarea name="Notes_999" class="cssDetail" id="Notes_999" autocomplete="off"> </textarea>

The Jquery code:

$(".cssDetail").bind("paste", function(e){
    // access the clipboard using the api
    var elemID = $(this).attr("id");
    console.log("elemID: " + elemID);
    var t_string = "";
    var pastedData = e.originalEvent.clipboardData.getData('text');
    console.log("pastedData " + pastedData );
    var arrayOfLines = pastedData.split('\n');
    $.each(arrayOfLines, function(index, item) {
        var cnt_spaces = item.search(/\S|$/);
        console.log("cnt_spaces: " + cnt_spaces + " - line: " + item);
        item = item.replace(/^\s+|\s+$/g, '');
        
        if (cnt_spaces == 8) {
            t_string +=  "- " + item + '\n';
        } else {
            t_string += item + '\n';
        }

    });
    
    //console.log("elemID: " + elemID);
    $("#"+elemID).text('');  // Try to clean before replace new content but not working.
    $("#"+elemID).text(t_string);
});

The function above works fine and does what I expected. However, the result inside the Textarea has both the original and the "modified " one (t_string variable). I only want the "modified" one in the textarea, please help.

Thank you,

Milacay
  • 1,407
  • 7
  • 32
  • 56
  • may be try `.val('')` here `$("#"+elemID).text('');` – Pirate Dec 14 '20 at 18:41
  • I just tried that, but it is not working. thanks for the suggestion. – Milacay Dec 14 '20 at 18:46
  • cool, it would be easy if you could post related html and a small reproducible example. – Pirate Dec 14 '20 at 18:47
  • I just updated the post with the HTML code (just a simple textarea). Thanks! – Milacay Dec 14 '20 at 18:55
  • looks like its a timing issue. I tried setting timeout `setTimeout(function() { $("#" + elemID).val(''); // Try to clean before replace new content but not working. }, 1000);` it cleared data after 1 sec. Unfortunately I not sure why this is an issue. but may be you could give it a try and find a work around. – Pirate Dec 14 '20 at 19:04
  • 1
    Something similar is done here. [check this out](https://stackoverflow.com/a/43439018) – Pirate Dec 14 '20 at 19:10
  • I tried that timing suggestion, it cleared data for both original and "modified" one. weird. Thanks for looking into this. – Milacay Dec 14 '20 at 19:21
  • You need to do both clear and update with modified data in timeout function otherwise one will run before than other. – Pirate Dec 14 '20 at 19:25
  • I tried that too, it will clear everything in 1 sec. Thanks for trying to help. – Milacay Dec 14 '20 at 20:40
  • Actually, I followed the Link you suggested and it works. Thank you very much! – Milacay Dec 14 '20 at 20:46
  • awesome. glad it helped, cheers! – Pirate Dec 14 '20 at 20:48

0 Answers0