Arish
Arish

Reputation: 342

Jquery add comma to a pasted string in input

I want to add a comma after copy pasting string into input

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

   var a=$("#keywords").val();
   alert(a);
  });

I tried the above code to check if the pasted value is passed into variable. In alert it is displaying empty value. How can i know if the value of the pasted string is passed into variable and then append comma to the variable.

Here is the fiddle

Upvotes: 1

Views: 1868

Answers (2)

Chandra Sekhar Walajapet
Chandra Sekhar Walajapet

Reputation: 2554

the paste event here fires / triggers before the actual content is pasted on to the control, if you do a timeout and find out what value is before and after paste you can figure out and add a ; to the end.

i found a similar thread on the same here Catch paste input and you can find the answer too.

Upvotes: 2

adeneo
adeneo

Reputation: 318212

The paste event is fired before the pasted text is available as a value property on the textbox. You need to wait a few milliseconds, and then just add the comma to the value, like so:

$("#keywords").on('paste', function() {
    var self = this, timer = setTimeout(function() {
        self.value=self.value+',';
    }, 300);
});​

FIDDLE

Upvotes: 1

Related Questions