? I cannot do keyup or keydown ...">

Onchange for input type = "number"

How can I handle onchange for <input type="number" id="n" value="5" step=".5" /> ? I cannot do keyup or keydown because the user can simply use the arrows to change the value. I would like to process it whenever it changes, and not just the blur, which, in my opinion, has the .change() event. Any ideas?

+61
jquery html5
Mar 07 2018-12-12T00:
source share
10 answers

Use mouse and keyboard

 $(":input").bind('keyup mouseup', function () { alert("changed"); }); 

http://jsfiddle.net/XezmB/2/

+108
Mar 07 2018-12-12T00:
source share

The oninput event ( .bind('input', fn) ) covers any changes from keystrokes to arrow clicks and keyboard / mouse inserts, but is not supported in IE <9.

 jQuery(function($) { $('#mirror').text($('#alice').val()); $('#alice').on('input', function() { $('#mirror').text($('#alice').val()); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input id="alice" type="number" step="any" value="99"> <p id="mirror"></p> 
+48
Mar 07 '12 at 22:12
source share

http://jsfiddle.net/XezmB/8/

 $(":input").bind('keyup change click', function (e) { if (! $(this).data("previousValue") || $(this).data("previousValue") != $(this).val() ) { console.log("changed"); $(this).data("previousValue", $(this).val()); } }); $(":input").each(function () { $(this).data("previousValue", $(this).val()); });โ€‹ 

It's a bit of a ghetto, but in this way you can use the โ€œclickโ€ event to capture an event that is executed when you use the mouse to zoom in / out with the small arrows on the input. You can see how I built a small manual procedure for checking changes, which ensures that your logic will not work if the value does not actually change (to prevent false positives from simple clicks in the field).

+6
Mar 07 '12 at 22:01
source share

To determine if a mouse or key is pressed, you can also write:

 $(document).on('keyup mouseup', '#your-id', function() { console.log('changed'); }); 
+4
Jun 07 '15 at 15:51
source share
 $(':input').bind('click keyup', function(){ // do stuff }); 

Demo: http://jsfiddle.net/X8cV3/

+3
Mar 07 2018-12-12T00:
source share

There may be a better solution, but this is what came to mind:

 var value = $("#yourInput").val(); $("#yourInput").on('keyup change click', function () { if(this.value !== value) { value = this.value; //Do stuff } }); 

Here is a working example .

It simply binds the event handler to the keyup , change and click events. It checks to see if the value has changed, and if so, it saves the current value so that he can check it again next time. To check the click event, a check is required.

+1
Mar 07 2018-12-12T00:
source share
 $("input[type='number']").bind("focus", function() { var value = $(this).val(); $(this).bind("blur", function() { if(value != $(this).val()) { alert("Value changed"); } $(this).unbind("blur"); }); }); 

OR

 $("input[type='number']").bind("input", function() { alert("Value changed"); }); 
+1
Mar 06 '14 at 11:10
source share
 <input type="number" id="n" value="0" step=".5" /> <input type="hidden" id="v" value = "0"/> <script> $("#n").bind('keyup mouseup', function () { var current = $("#n").val(); var prevData = $("#v").val(); if(current > prevData || current < prevData){ $("#v").val(current); var newv = $("#v").val(); alert(newv); } }); </script> 

http://jsfiddle.net/patrickrobles53/s10wLjL3/

I used the hidden input type as a container of the previous value, which will be needed for comparison at the next change.

+1
Feb 04 '15 at 8:08
source share

Since $("input[type='number']") does not work in IE, we must use the class name or id, for example $('.input_quantity') .

And do not use the .bind() method. The .on() method is the preferred method for attaching event handlers to a document.

So my version is:

HTML

 <input type="number" value="5" step=".5" min="1" max="999" id="txt_quantity" name="txt_quantity" class="input_quantity"> 

JQuery

 <script> $(document).ready(function(){ $('.input_quantity').on('change keyup', function() { console.log('nice'); }); }); </script> 
+1
Oct. 10 '15 at 15:07
source share

I had the same problem and decided to use this code

HTML

 <span id="current"></span><br> <input type="number" id="n" value="5" step=".5" /> 

You can add only 3 first lines, other parts are optional.

 $('#n').on('change paste', function () { $("#current").html($(this).val()) }); // here when click on spinner to change value, call trigger change $(".input-group-btn-vertical").click(function () { $("#n").trigger("change"); }); // you can use this to block characters non numeric $("#n").keydown(function (e) { // Allow: backspace, delete, tab, escape, enter and . if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 || (e.keyCode === 65 && e.ctrlKey === true) || (e.keyCode >= 35 && e.keyCode <= 40)) return; if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) e.preventDefault(); }); 

Example here: http://jsfiddle.net/XezmB/1303/

+1
Jul 13 '17 at 21:02
source share



All Articles