NedStarkOfWinterfell
NedStarkOfWinterfell

Reputation: 5203

Paste event anomaly with jQuery in Opera

I am trying out this code. What it does is that when somebody right clicks on the first text box, shift the focus to the second, so that if he selects the Paste option from the context menu, the text is pasted in the second box. A paste event has been attached to the 2nd box to alert Foo.

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){

  $('.foo').contextmenu(function() {
      $('.boo').select();
      return true;
  });
$(".boo").bind("paste",function() { alert("Foo"); });
});
</script>
</head>
<body>
<input type = 'text' class = 'foo' />
<input type = 'text' class = 'boo' />
</body>
</html>

It works fine in all browsers except Opera. When you right click and paste some text in the first box, the alert is popped for all these browsers. In Opera, if you paste some text directly in the 2nd box, the alert pops up all right. But if you do it via the first box, i.e shifting the focus to the 2nd while right-clicking on the first, so that the clipboard text gets pasted in the 2nd, it doesn't pop the alert, even though you can clearly see that the 2nd box has got the focus, and the text gets pasted there just fine. Can anyone tell me what is the problem, and how to fix it?

Upvotes: 1

Views: 367

Answers (1)

Bergi
Bergi

Reputation: 665256

As you can see in this fiddle, Opera fires the paste event on the element that was right-clicked on (while inserting the text where the focus was moved to). However, as you can see here, the input event fires on the element where you had expected it.

Can anyone tell me what is the problem, and how to fix it?

You are moving the selection/focus during the paste action (two clicks), which confuses both user and browser :-). I'm not sure how to fix this anomaly, but you certainly don't want move pastes to another input only if they were done via the contextmenu anyway.

Upvotes: 1

Related Questions