Type onChange event on input type number

I have an input type number. I know how to call a function when I press the enter key.

Please see my code:

<input type="number" class="new_num" value="0"> <input type="number" class="new_num_2" value="0"> <script> $('.new_num').bind("enterKey",function(e) { alert($(".new_num").val()); }); $('.new_num').keyup(function(e){ if(e.keyCode == 13) { $(this).trigger("enterKey"); } }); </script> 

Now I need to warn the value when the user simply presses the up or down arrow on the number. I wrote this code, but it did not work:

 $('.new_num').bind('keyup input', function(){ $(this).trigger("enterKey"); }); 

Please, help.

Also I need to do the same operation for new_num_2 . Do I need to duplicate the code?

What code do I need to write if the user is using a mobile device?

+5
source share
1 answer

This example registers the value of the current focused input in the console. I gave them the same class=new_num_2 and used them to select them. Thus, you do not need to duplicate the code and perform the same operation for new_num_2 .

For mobile phones, I think you should put a button after input and bind this function to a button click.

 $('.new_num').on("focus", printValue); var printValue = function (inp){ $(inp).bind("enterKey",function(e) { console.log($(this).val()); }); } $('.new_num').keyup(function(e){ if(e.keyCode == 13){ printValue(this); $(this).trigger("enterKey"); $(this).unbind("enterKey"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="number" class="new_num" id="new_num_1" value="0"> <input type="number" class="new_num" id="new_num_2" value="0"> 
+3
source

Source: https://habr.com/ru/post/1261863/


All Articles