How to get all of the pasted string, in input which has a maxLength attribute?
You can access clipboardData
through function getData()
, and print it instead of e.target.value()
. If you store it in a temporary variable, like I did in my example, you are able to perform further elaboration on the pasted string.
It works for reasonably recent versions of browsers (for example FF 22+).
const onPasteFn = (e) => {
var myData = e.clipboardData.getData("text/plain");
setTimeout(() => document.getElementById("demo").innerHTML = myData, 0)
}
<input type="text" maxLength="5" onpaste="onPasteFn(event)" />
<p id="demo"></p>
Consider using clipboardData
from the event, where you can use getData()
to grab the text that was pasted from the clipboard like so:
const onPasteFn = (e) => {
document.getElementById("demo").textContent = (e.clipboardData || window.clipboardData).getData('text');
}
<input type="text" maxLength="5" onpaste="onPasteFn(event)" />
<p id="demo"></p>
See example here from the docs. Note that the fallback of || window.clipboardData
is used for IE support.