0

I use a script to detect url in text when someone paste a text with URL.

So I use this script:

$("#post").bind('paste', function(){

   var $ptext = $('textarea#post').val();
   var regexText = /^http:\/\/www$/;
   if(regexText.test($ptext))
   {
    alert('URL DETECTED');
   }
});

But when I reload the page and I paste the text for the first time the script does nothing and I'm forced to repaste the text again, and then the script works.

How do I solve this problem?

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
Samy Reef
  • 73
  • 1
  • 9

1 Answers1

0

I don't know why, but for some reason .bind() needs a setTimeout(function(){ //now do the textarea val() call;}, 100); in order to access the updated paste event...but if you use .on() instead, it will work without the setTimeout call

here's a working example showing how to achieve this in either scenario

thanks to @Ohgodwhy

Samy Reef
  • 73
  • 1
  • 9