1

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

Arish
  • 254
  • 3
  • 5
  • 14

2 Answers2

2

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.

Community
  • 1
  • 1
  • Thanks for reminding me about this logic, i was not aware of this i.e paste event is triggered before the content is pasted. – Arish Aug 30 '12 at 06:46
1

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

adeneo
  • 312,895
  • 29
  • 395
  • 388