user2197789
user2197789

Reputation: 103

Trigger a function on key up on any input

I want a javascript function to be triggered on key up on any input box. I tried using getElementsByTagName.onkeyup but it didn't work. Along with the solution please send a working jsfiddle. I don't want document.getElementById or onkeyup="function()" as there are many input boxes. It wont look tidy. I even tried

$(document).ready(function(){
$('input').onkeyup(function(){
calculate();
})
})

I also want a function that will add 0 to the value of each input on window.onload. Thank You

Upvotes: 0

Views: 82

Answers (4)

Arun P Johny
Arun P Johny

Reputation: 388316

Try

$(document).ready(function() {
    $(document).on('keyup', 'input', function() {
        calculate();
    })
})

Demo: Fiddle

Upvotes: 0

underpaid_pedro
underpaid_pedro

Reputation: 75

This should do the keyup part:

http://jsfiddle.net/pwqx7/

$(function() {
$('input').bind('keyup', function() {
      alert($(this).attr('id'));
});
});

Upvotes: 0

user2294813
user2294813

Reputation: 1

try

document.onkeyup = function () {
  if (document.activeElement.tagName.toUpperCase() !== 'input') return;
  // do sth. here
}

Upvotes: 0

PSR
PSR

Reputation: 40318

DEMO There is no onkeyup event in jQuery tru to change to keyup

 $(document).ready(function(){
      $('input').keyup(function(){
      calculate();
      })
    })

.keyup()

Upvotes: 1

Related Questions