Apparently there are some situations where jQuery doesn't work for pasting. You might need to bind to several events as shown in following reference to catch users pasting contents accurately.
From Soliciting Fame - jQuery keyup vs bind - (from way-back machine)
// detect the change
$('input#myId').bind("change keyup input",function() {
// handle events here
});
UPDATE: bind was deprecated on Jquery 3.0 and was replaced with on
// detect the change
$('input#myId').on("change keyup input",function() {
// handle events here
});
Answer from jwwishart on Stack Overflowon("keyup paste", function) not updating in Microsoft Edge
Trigger the keyup function when someone copy and paste
Can .change handle immediate text changes?
Simulate keyup event when data pasted into input box
Apparently there are some situations where jQuery doesn't work for pasting. You might need to bind to several events as shown in following reference to catch users pasting contents accurately.
From Soliciting Fame - jQuery keyup vs bind - (from way-back machine)
// detect the change
$('input#myId').bind("change keyup input",function() {
// handle events here
});
UPDATE: bind was deprecated on Jquery 3.0 and was replaced with on
// detect the change
$('input#myId').on("change keyup input",function() {
// handle events here
});
In this particular situation enough will be just bind to the input event:
$('textarea').bind("input", function() {});
Switch the event keyup for input, which will trigger whenever something is inputted into the field, even if text is being pasted (both by pressing CTRL + V or right mouse button ยป Paste.
.on('input', '.emotion', function() {
// Do your stuff.
});
This will do what you want:
$("#editor").on('paste', function(e) {
$(e.target).keyup();
});
$("#editor").on('keyup', function(e) {
alert('up');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="editor">
This jQuery code catches immediate changes to any element, and should work across all browsers:
$('.myElements').each(function() {
var elem = $(this);
// Save current value of element
elem.data('oldVal', elem.val());
// Look for changes in the value
elem.bind("propertychange keyup input paste", function(event){
// If value has changed...
if (elem.data('oldVal') != elem.val()) {
// Updated stored value
elem.data('oldVal', elem.val());
// Do action
....
}
});
});
So, you have to wait for these events: propertychange keyup input paste
Try using .focusOut(), this should handle all events ass soon as the user leaves the textbox, regardless of click, drops, tabs etc.
$('#my_input').on('focusout', function() {
$(this).val($(this).val().replace(/ +?/g, ' '));
});
http://api.jquery.com/focusout/
If you want to capture all input events:
$('#username').on("input", username_check);
Use .on() or .bind() to bind multiple events,
$(document).ready(function(){
$('#username').on('keyup paste',username_check);
});
function username_check(){
setTimeout( function() {
var username = $('#username').val();
},100);
if(username == "" || username.length < 4){
alert("error");
}
}
Working Fiddle
You can use $.on() function and attach multiples handlers.
$("#element").on('keyup change', function (){
// Your stuff...
});
You can use only "input" event if you don't need to provide support for the older browsers. It will be fired everytime a value changes in the input element. Here's a list of the supported browsers: https://developer.mozilla.org/en-US/docs/Web/Events/input
There is an onpaste event that works in modern day browsers. You can access the pasted data using the getData function on the clipboardData object.
$("#textareaid").bind("paste", function(e){
// access the clipboard using the api
var pastedData = e.originalEvent.clipboardData.getData('text');
alert(pastedData);
} );
Note that bind and unbind are deprecated as of jQuery 3. The preferred call is to on.
All modern day browsers support the Clipboard API.
See also: In Jquery How to handle paste?
How about this: http://jsfiddle.net/5bNx4/
Please use .on if you are using jq1.7 et al.
Behaviour: When you type anything or paste anything on the 1st textarea the teaxtarea below captures the cahnge.
Rest I hope it helps the cause. :)
Helpful link =>
How do you handle oncut, oncopy, and onpaste in jQuery?
Catch paste input
EDIT:
Events list within .on() should be space-separated. Refer https://api.jquery.com/on/
code
$(document).ready(function() {
var $editor = $('#editor');
var $clipboard = $('<textarea />').insertAfter($editor);
if(!document.execCommand('StyleWithCSS', false, false)) {
document.execCommand('UseCSS', false, true);
}
$editor.on('paste keydown', function() {
var $self = $(this);
setTimeout(function(){
var $content = $self.html();
$clipboard.val($content);
},100);
});
});
OK, just bumped into the same issue.. I went around the long way
$('input').on('paste', function () {
var element = this;
setTimeout(function () {
var text = $(element).val();
// do something with text
}, 100);
});
Just a small timeout till .val() func can get populated.
E.
You can actually grab the value straight from the event. Its a bit obtuse how to get to it though.
Return false if you don't want it to go through.
$(this).on('paste', function(e) {
var pasteData = e.originalEvent.clipboardData.getData('text')
});
Change keypress to keyup and it should work.
Tested, working. Fiddle: http://jsfiddle.net/fnkr/MXnuk/
<script type="text/javascript">
function checkInput() {
if (document.getElementById("field_ygtw9u").value.length < 14) { //disable submit if "no" is selected
$('input[type=submit]').attr("disabled", "disabled");
} else { //enable the submit button
$('input[type=submit]').removeAttr('disabled');
}
}
jQuery(document).ready(function ($) {
$('input[type=submit]').attr("disabled", "disabled");
$('input[name="item_meta[1234]"]').bind("input", function () { //change # to the ID of your field
checkInput();
});
$('input[name="item_meta[1234]"]').bind("propertychange", function () { //change # to the ID of your field
checkInput();
});
});
</script>
<form>
<div id="frm_field_1234_container" class="frm_form_field form-field frm_required_field frm_top_container">
<label class="frm_primary_label">Minimun 15 char: <span class="frm_required"></span>
</label>
<input type="text" id="field_ygtw9u" name="item_meta[1234]" value="" maxlength="15"
class="required" />
<p class="submit">
<input type="submit" value="Submit" />
</p>
</form>