2

I want to check whether the URL has been input into textarea or not. This is a function that checks textarea:

$('#text textarea').on('input paste', function() {
            checkUrl($(this));
        });

Only one URL is allowed, so after finding the URL handler is unbind using off:

function checkUrl(elem) {

var words = elem.val().split(/\s+/);
$.each(words, function(index, element) {
    if (isValidURL($.trim(element))) {          
        $.ajax({...}); // Ajax call
        elem.off('input paste');
        return;
    }
});

}

Function checkUrl is called not only once but twice after pasting the URL (CTRT+V). Ajax is also triggered twice.

Thanks a lot in advance.

EDIT

I've changed my code according to this:

$('#text textarea').bind('paste', function() {
        var _this = $(this);
        setTimeout( function() {
            checkUrl(_this);
        }, 100);
    });

It's now working with paste handler only, but the main problem persists...

Community
  • 1
  • 1
peter.o
  • 2,910
  • 6
  • 47
  • 73
  • `input` includes all methods of input, including pasting. Is there a reason for explicitly adding `paste`? – pimvdb Oct 23 '12 at 07:24
  • No, I've just seen "input paste" here http://stackoverflow.com/questions/686995/jquery-catch-paste-input – peter.o Oct 23 '12 at 07:29
  • I've tried input only, but it's the same – peter.o Oct 23 '12 at 07:30
  • "CTRL" and "V" might cause the problem... – peter.o Oct 23 '12 at 07:38
  • Please paste your HTML code, or create a JSFiddle replicating your problem. It'll help us to help you. – Andreas Eriksson Oct 23 '12 at 08:55
  • Well, it's not that easy :) I've found out that this "doble trigger" error happens only first time, when the page is loaded – peter.o Oct 23 '12 at 09:25
  • I can't reproduce this issue on Chrome, nor when the page is first loaded. See http://jsfiddle.net/8SgYH/. What browser/OS are you using? – pimvdb Oct 23 '12 at 17:39
  • Win7 32b, FF latest. I think there are two same handlers for pasting...but I can't find them... – peter.o Oct 24 '12 at 06:40
  • I cannot reproduce in Firefox 16. If I only bind `input`, then it's always triggered once, as expected. Does the fiddle show the issue for you? If not, there must be something else happening in your code. – pimvdb Oct 24 '12 at 11:29

2 Answers2

1

Get the event object and check for the event type 'input' so if its a paste event it will trigger the input event

you can try this

$('#text textarea').on('input paste',function(event){


    if(event.type=='input'){
       checkUrl($(this));
    }
 });​

demo: http://jsfiddle.net/nLPrG/

fuzionpro
  • 581
  • 4
  • 18
0

I think I have solved the problem. Before binding events I´ve unbound everything from the element.

$('#text textarea').unbind();   
$('#text textarea').bind('paste', function() {
        var _this = $(this);
        setTimeout( function() {
            checkUrl(_this);
    }, 100);
})
jcubic
  • 51,975
  • 42
  • 183
  • 323
peter.o
  • 2,910
  • 6
  • 47
  • 73