top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Using onpaste() event in Javascript or Jquery how to get window.getSelection() value?

0 votes
1,995 views

I tried the following code.Both onpaste() and Oncopy() will trigger when user trying to copy or paste.

But my problem is while copying something window.getSelection() not working.

Window.getSelection() is used to get the selected data.

I mean before copying something user need to select some values.So,using this window.getselection() we will get the value what they are selected.

Here, my problem is window.getselection() will work in oncopy but not working in onpaste.

Check the below examples and help me to solve the problem.

Also ,is there any alternative method also please let me know.I also tried window.clipboardData.But not getting the solution for this.

<!doctype html>
<html>
<head>
<style>
    textarea {
        width:500px;
        height:200px;
        border:2px solid #999;
    }
</style>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    <script type="text/javascript">

        function OnPaste () {                                                                       
            var selection = window.getSelection();          
            alert(selection.toString());            
        }                                       
        function OnCopy() {         
            var selection = window.getSelection();
            alert(selection.toString());
         }

// Call the OnCopy function while user trying to copy 
document.oncopy = OnCopy;

// Call the OnPaste function while user trying to Paste
document.onpaste = OnPaste;
 </script>

</head>
<body>
    Select some text, copy it to the clipboard and try to paste it to the following fields:
    <br /><br />
    <textarea size="40" value="Copy and Paste operation is enabled">
posted Jul 4, 2014 by Sidharth Malhotra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Window.getselection() is used to get the selected text.so while you coping you will select something.So that time window.getselection() will work and it will show you the selected text.

But it wont work inside the paste event.Because while pasting you won't select anything. So window.getselection() will not work in the paste event.

window.clipboardData also not work in all browsers.However, in other browser you may need to use flash to get the content, since there is no standard interface to access the clipboard. May be you can have try this plugin Zero Clipboard .

answer Aug 12, 2014 by Pankaj Deshmukh
Similar Questions
+3 votes

I am having textbox and submit and close button. There is jquery validation on focusout of textbox. Which is fine. But when user type something in textbox and directly click the Close button i don't want to do validation. As focusout event fired first so validation work. I want button click event to fire first.

+5 votes

I need disable all refresh events

Example.

  1. F5,
  2. Mouse right click option (refresh/reload),
  3. CTRL+R,
  4. Menu bar -> view -> refresh and
  5. URL refresh/reload button

How to disable these all events by using javascript...

Advance thanks....

0 votes

Also,tell me what are the types of framework are there ?

0 votes

I tried the following code.But its not working.

$('#id').mouseover(function(e) {
 setTimeout('alert("Mouse Over Called")', 5000);
}
0 votes

Please give the example for both.

...