73342

jQuery get the value of CUT

Question:

As you may see in the snippet I'm able to get the value of the string that is PASTED in the input. I was wondering if there's a simple way to get the value of the CUTED string.

<pre class="snippet-code-js lang-js prettyprint-override"> $('#example').on("paste cut", function(e) { var value = ''; if(e.type == 'paste') { value = e.originalEvent.clipboardData.getData('text'); } else if(e.type == 'cut') { // How should I get the removed part of the string } $('#result').html(value); }); <pre class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <input type="text" id="example" value="Some random value" style="width:80%"></div> <div id="result"></div>

Answer1:

How about this solution using window.getSelection().toString():

<pre class="snippet-code-js lang-js prettyprint-override"> $('#example').on("paste cut", function(e) { var value = e.type=='paste' ? e.originalEvent.clipboardData.getData('text') : window.getSelection().toString(); console.log(e.type+': '+value); $('#result').html(value); }); <pre class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <input type="text" id="example" value="Some random value" style="width:80%"></div> <div id="result"></div>

Recommend

  • how to clear the textarea in onpaste method?
  • Single function call on scroll event?
  • TypeError $(…) is not a function when using a jquery plugin
  • jQuery Detect Paste Event Anywhere on Page and “Redirect” it to Textarea
  • How to stop the touchend event firing on touchstart with backbone
  • Getting urldata from an image as a blob
  • Serializing a list of Object using Json.NET
  • c# How to detect and process a .URL file type on DragDrop event
  • RxJava,Retrofit Error :Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
  • Facebook iOS SDK Not Calling Completion Handler
  • Image map in Flex
  • Javascript, Regex - I need to grab each section of a string contained in brackets
  • ASP.NET MVC Application won't update some controllers
  • What's the purpose of QString?
  • How can I speed up CURL tasks?
  • Install PHP intl extension on MacOS
  • Combining SpatialPolygonsDataFrame of two neighbour countries
  • Refering to the class itself from within a class mehod in Objective C
  • Nant, Vault & Windows Integrated Authentication
  • What is Eclipse's Declaration View used for?
  • MySQL WHERE-condition in procedure ignored
  • Updated Ionic CLI but shows previous version (Windows)
  • Web-crawler for facebook in python
  • Jquery - Jquery Wysiwyg return html as a string
  • SVN: Merging two branches together
  • php design question - will a Helper help here?
  • AngularJs get employee from factory
  • trying to dynamically update Highchart column chart but series undefined
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Append folder name and increment by 1 using batch script
  • JaxB to read class hierarchy
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal