Changing function parameters for onkeypress event according to source id

Posted by DarRay on Stack Overflow See other posts from Stack Overflow or by DarRay
Published on 2011-01-06T17:44:22Z Indexed on 2011/01/06 17:53 UTC
Read the original article Hit count: 261

I want to assign a function according to their id to all the input fields in a webpage. To do i wrote below code but all the input fields are running keyPress with same parameter.. :(

///////////////////Checks all available 'text' 'input's////////////////////
var inputs = document.getElementsByTagName('input');
var cnvtrInput = new Array();
for (var index = 0; index < inputs.length; index++) {
 if (inputs[index].type == 'text') {
  cnvtrInput[index] = new converter(inputs[index]);
  inputs[index].onkeypress = function() {return keyPess(cnvtrInput[index])};
 }
}
//index--;

With the last commented statement i found that the passing element of keyPress is the last value of index;

Finally i tried same with textareas but failed...

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about events