You can bind to multiple events by separating them with a space:

$(":input").on("keyup change", function(e) {
    // do stuff!
})

docs here.

Answer from keeganwatkins on Stack Overflow
Top answer
1 of 7
237

You can bind to multiple events by separating them with a space:

$(":input").on("keyup change", function(e) {
    // do stuff!
})

docs here.

2 of 7
25

If you're ever dynamically generating page content or loading content through AJAX, the following example is really the way you should go:

  1. It prevents double binding in the case where the script is loaded more than once, such as in an AJAX request.
  2. The bind lives on the body of the document, so regardless of what elements are added, moved, removed and re-added, all descendants of body matching the selector specified will retain proper binding.

The Code:

// Define the element we wish to bind to.
var bind_to = ':input';

// Prevent double-binding.
$(document.body).off('change', bind_to);

// Bind the event to all body descendants matching the "bind_to" selector.
$(document.body).on('change keyup', bind_to, function(event) {
    alert('something happened!');
});

Please notice! I'm making use of $.on() and $.off() rather than other methods for several reasons:

  1. $.live() and $.die() are deprecated and have been omitted from more recent versions of jQuery.
  2. I'd either need to define a separate function (therefore cluttering up the global scope,) and pass the function to both $.change() and $.keyup() separately, or pass the same function declaration to each function called; Duplicating logic... Which is absolutely unacceptable.
  3. If elements are ever added to the DOM, $.bind() does not dynamically bind to elements as they are created. Therefore if you bind to :input and then add an input to the DOM, that bind method is not attached to the new input. You'd then need to explicitly un-bind and then re-bind to all elements in the DOM (otherwise you'll end up with binds being duplicated). This process would need to be repeated each time an input was added to the DOM.
🌐
jQuery
api.jquery.com › keyup
keyup event | jQuery API Documentation
Bind an event handler to the "keyup" event, or trigger that event on an element.
🌐
W3Schools
w3schools.com › jquery › event_keyup.asp
jQuery keyup() Method
jQuery Overview jQuery Selectors jQuery Events jQuery Effects jQuery HTML/CSS jQuery Traversing jQuery AJAX jQuery Misc jQuery Properties ... The keyup event occurs when a keyboard key is released. The keyup() method triggers the keyup event, ...
Top answer
1 of 2
43

They are almost the same thing. In jQuery or JavaScript, I would have to recommend the change() event. The reason you should not use keyup() is because if a user inputs a value using autofill, it will not fire the keyup() event. However, autofill does fire the change() event, and your verification script will run, and the input will be verified.

NOTE: Normally, change() is fired when the element loses focus. If you want to check data after every key press, you could combine keyup() and change(), which would allow you to parse the data on both events. This is the best of both worlds in my opinion.

Hope this helps!

2 of 2
9

Use keyup with debouncing, it's more user friendly.


keyup

Whenever you release a key.

The keyup event is sent to an element when the user releases a key on the keyboard. It can be attached to any element, but the event is only sent to the element that has the focus. Focusable elements can vary between browsers, but form elements can always get focus so are reasonable candidates for this event type. -- http://api.jquery.com/change/


change

Whenever the content of that field changes, generally, it happens when you remove focus from that field, but not only that.

The change event is sent to an element when its value changes. This event is limited to input elements, textarea boxes and select elements. For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus. -- http://api.jquery.com/change/


Use keyup and a debounced callback So that you verification process isn't fired after each key stroke, but only if the user stops typing, check this example: http://codepen.io/desandro/full/JDugF, open the page, open the javascript console, and start scrolling.

🌐
CodePen
codepen.io › vezi › pen › xwMJaE
jQuery keyup change event
var keyup_count = 0; var change_count = 0; $('#input1').keyup(function(){ keyup_count++; $('#log_keyup').html('keyup:'+keyup_count) }) $('#input1').change(function(){ $('#log_change').html('change:'+change_count) })
🌐
Steemit
steemit.com › utopian-io › @alexendre-maxim › jquery-tutorial-02-keyboard-and-form-events-keyup-keydown-keypress-change-focus-and-blur
jQuery Tutorial #02 Keyboard and Form Events ( Keyup, Keydown, Keypress, Change, Focus and Blur) — Steemit
August 7, 2018 - How to use the keyup event. How to use the keydown and keypress events, what's the difference between them. How to use the change event. What's the focus and blur events and how to use them. ... In this tutorial we will continue the jQuery events , we will take the keyboard and form events. - I created a table to organize the tutorial and to clarify the points more, in the table I have 5 columns , each one ...
🌐
Edureka Community
edureka.co › home › community › categories › web development › jquery › jquery if change or keyup
jQuery if change or keyup | Edureka Community
June 20, 2022 - Using jQuery i would like to run a function when either .change() or .keyup() are raised. Something like ... () ) { alert( 'something happened!' ); }
🌐
Javatpoint
javatpoint.com › jquery-keyup
jQuery keyup() - javatpoint
It works only on form fields. When the change event occurs, the change () method attaches a function with it to run. Note: This event is limited to <input> elements, <textarea> boxes and <select>... ... The unbind() method in jQuery is used to remove the event handlers from the selected elements. We can also use this method to remove all or specific event handlers.
Find elsewhere
🌐
Web-profile
web-profile.net › jquery › dev › jquery-input-value-change-keyup
jQuery change input value on keyup – web-profile
$(function(){ $('form input').data('val', $('form input').val() ); // save value $('form input').change(function() { // works when input will be blured and the value was changed // console.log('input value changed'); $('.log').append(' change'); }); $('form input').keyup(function() { // works immediately when user press button inside of the input if( $('form input').val() != $('form input').data('val') ){ // check if value changed $('form input').data('val', $('form input').val() ); // save new value $(this).change(); // simulate "change" event } }); });​
🌐
JSFiddle
jsfiddle.net › krainey › UhR85
jQuery keyup vs keydown vs change - JSFiddle - Code Playground
The Code Completion will now also have the context of all panels before suggesting code to you - so if for example you have some CSS or JS, the HTML panel will suggest code based on the other two panels.
🌐
Infoheap
infoheap.com › home › tutorials › javascript › jquery
jQuery - text input field - change keyup and paste events - InfoHeap
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <input id=field1 type=text value=10> <div id=msg></div> <script type="text/javascript"> $('input#field1').on("change keyup paste", function(evt) { $("#msg").append("eventtype => " + evt.type + " / newval => " ...
🌐
jq recipes
remysharp.com › 2009 › 07 › 08 › jquery-multibinding-events
jQuery multibinding events
August 8, 2009 - With multibinding, you can bind the same event handler to more than one event: $('input').bind('keyup change', function () { var i = 255-this.value, j = this.value; $('body').css('background', 'rgb(' + [i,i,j].join(',') + ')'); }); Published 8-Jul 2009 under #code & #jquery & #plugin.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › jquery tutorial › jquery keyup()
jQuery keyup() | Implementation of jQuery keyup() Method
March 20, 2023 - Once any key is pressed down inside the input field, the background color changes to blue as shown in the below screenshot. After the pressed key is released, the background color of the input field changes to teal as shown below. Now, consider a different example demonstrating the effect of keyup() method. <html> <head> <title>Example for jQuery keyup()</title> <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <style> div { width: 800px; height: 500px; padding: 20px; font-size: medium; text-align: center; margin: auto; font-weight: bold; border: 3px solid cornflowerblue; ma
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
BeginnersBook
beginnersbook.com › 2019 › 05 › jquery-keyup-event
jQuery keyup() Method
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"> </script> <script> $(document).ready(function(){ $("input").keydown(function(){ $("input").css("background-color", "green"); }); $("input").keyup(function(){ $("input").css("background-color", "yellow"); }); }); </script> </head> <body> <h2>jQuery keyup event example on Beginnersbook.com</h2> Write anything here: <input type = "text"> <p>The background color of input text field will change to green, when you press a key inside the input field and the color will change to yellow when you release the key inside the input text field.</p> </body> </html> Output: Before a key is released: This screenshot is taken when the page is loaded in browser and no activity is performed on the page.
🌐
GeeksforGeeks
geeksforgeeks.org › jquery-keyup-method
jQuery keyup() Method - GeeksforGeeks
July 7, 2023 - <!DOCTYPE html> <html> <head> <title>Jquery | Keyup() </title> <script src= "https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script> <script> let colors = ['red', 'blue', 'green', 'grey', 'black', 'white', 'teal', 'yellow']; let i = 0; $(document).keyup(function (event) { $('body').css('background-color', colors[i]); i++; i = i % 9; }); </script> </head> <body> <center> <h3> Press any key from the keyboard and then release it <br> to change the background color of the page </h3> </center> </body> </html> ... The jQuery bind() method is used to attach one or more event handlers for selected elements.
🌐
GitHub
github.com › reu › jquery.value.change.event
GitHub - reu/jquery.value.change.event: The mix of onkeyup and onchange events.
Jquery 'change' event only triggers when the input is blurred. You could use the 'keyup' event, but then everytime the key is up, it would trigger the event. This plugin introduces the 'valuechange' event, that works like a keyup, but only triggers ...
Author   reu
🌐
jQuery
api.jquery.com › keypress
keypress event | jQuery API Documentation
Bind an event handler to the "keypress" event, or trigger that event on an element.
🌐
jQuery
api.jquery.com › keyup-shorthand
.keyup() | jQuery API Documentation
Instead of .keyup( handler ) or .keyup( eventData, handler ), use .on( "keyup", handler ) or .on( "keyup", eventData, handler ), respectively.