JTJohnston
JTJohnston

Reputation: 95

Is there an easier way to call these functions using jQuery?

Instead of using

<input value="" type=text id="DPRnumber" onkeydown="javascript:backspacerDOWN(this,event);" onkeyup="javascript:backspacerUP(this,event);">

Is there an easier way to call these functions using jQuery? Something like:

$('#DPRnumber').on('onkeydown', 'input', function(e) { ... I don't know ...

Upvotes: 1

Views: 152

Answers (1)

Aiias
Aiias

Reputation: 4748

You have the right idea. Use jQuery's .on() with multiple events.

Try this:

$('#DPRnumber').on({
  keydown: function(event) {
    backspacerDOWN(this, event);
  },
  keyup: function(event) {
    backspacerDOWN(this, event);
  }
});

Edit:

Although we do not know what exactly your backspacerDOWN() function is doing, if you do not want to use this, then just replace all occurrences of this with document.getElementById('DPRnumber') like this:

$('#DPRnumber').on({
  keydown: function(event) {
    backspacerDOWN(document.getElementById('DPRnumber'), event);
  },
  keyup: function(event) {
    backspacerDOWN(document.getElementById('DPRnumber'), event);
  }
});

Upvotes: 1

Related Questions