Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

detect paste in textarea

function getTextAreaSelection(textarea) {
    var start = textarea.selectionStart, end = textarea.selectionEnd;
    return {
        start: start,
        end: end,
        length: end - start,
        text: textarea.value.slice(start, end)
    };
}

function detectPaste(textarea, callback) {
    textarea.onpaste = function() {
        var sel = getTextAreaSelection(textarea);
        var initialLength = textarea.value.length;
        window.setTimeout(function() {
            var val = textarea.value;
            var pastedTextLength = val.length - (initialLength - sel.length);
            var end = sel.start + pastedTextLength;
            callback({
                start: sel.start,
                end: end,
                length: pastedTextLength,
                text: val.slice(sel.start, end)
            });
        }, 1);
    };
}

var textarea = document.getElementById("your_textarea");
detectPaste(textarea, function(pasteInfo) {
    alert(pasteInfo.text);
    // pasteInfo also has properties for the start and end character
    // index and length of the pasted text
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: There is only one value in JavaScript that is not equal to itself, and that is NaN (“not a number”). 
Javascript :: how to connect two model in mongoose 
Javascript :: limit ajax request 
Javascript :: how to change div style to full page react 
Javascript :: cypress replace response part 
Javascript :: check if element is displayed 
Javascript :: responsive varient in material ui 
Javascript :: jquery show div class 
Javascript :: copy current filename in emacs 
Javascript :: prepare webpack-ready 
Javascript :: bootstrap dropdown with state 
Javascript :: simple form in react native with code 
Javascript :: declerative and imperative program combine 
Javascript :: js destructuring arguments 
Javascript :: math.factorial javascript 
Javascript :: regex placa de carro 
Javascript :: what is jsonpickle in python 
Javascript :: js remove item on index 
Javascript :: Backbone Model Vs Backbone Collection 
Javascript :: volta node list 
Javascript :: formart japanese date in js 
Javascript :: Changing Prototype 
Javascript :: stuck at "resvoling packages" 
Javascript :: toast.toastAlert ext js 
Javascript :: javascript get minutes between two dates 
Javascript :: remove the bottom selection line from materail ui 
Javascript :: async data nuxt multiple requests 
Javascript :: oop js 
Javascript :: Obtener url base 
Javascript :: ArrayReplace 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =