-2

I have input with type number:

<input type="number" id="input-number" onChange="DoSomething();"/>

How to fire onChange event with jQuery?

[EDIT]

Did it with following code:

$("#input_0").trigger("change");
Peter94
  • 33
  • 7
  • What do you mean by 'Fire'? Do you want jQuery to trigger the event, i.e. make an event, where there is no user event; or do you want to react on a user event? – yunzen Mar 14 '19 at 13:24

2 Answers2

-1

With trigger

Any event handlers attached with .on() or one of its shortcut methods are triggered when the corresponding event occurs. They can be fired manually, however, with the .trigger() method. A call to .trigger() executes the handlers in the same order they would be if the event were triggered naturally by the user:

console.clear()
DoSomething = (e) => {
  console.log(e.target)
}

$('#input-number').trigger('change')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<input type="number" id="input-number" onChange="DoSomething(event);"/>
yunzen
  • 32,854
  • 11
  • 73
  • 106
-1

Please have a look attached example.

You need to setup the jquery function ($(selector).change()) which is fired when lost the focus from textbox.

$("#input-number").on('change',function(){
  alert($(this).val());
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" id="input-number"/>
Smithiam
  • 162
  • 1
  • 16