javascript - passing array values into a function -


in script below, console.log returning correct values, , both alerts fire. however, value coming undefined.

any ideas i'm doing wrong?

jquery(document).ready(function(){      jquery("#customfield_21070").attr('style','width:60px');     jquery("#customfield_21070").attr('disabled','disabled');      var customfields = [     '#customfield_11070',     '#customfield_11071',     '#customfield_20071',     '#customfield_20072',        '#customfield_20073',     '#customfield_20074',     ];      for(var i=0, len=customfields.length; i<len; i++) {         console.log(customfields[i]);         jquery(customfields[i]).keyup(function(){             alert('calculate');//fires             calculatesum();         });     }       function calculatesum() {          var sum = 0;          alert('value: ' + this.value);//undefined          if(!isnan(this.value) && this.value.length!=0 && this.id !== "customfield_21070") {             sum += parsefloat(this.value);         }          jquery("#customfield_21070").val(sum.tofixed(2));     }  }); 

use function.prototype.call():

calls function given this value , arguments provided individually.

its first parameter thisarg:

the value of this provided call fun. note this may not actual value seen method: if method function in non-strict mode code, null , undefined replaced global object, , primitive values boxed.

so, pass this handler function calculatesum so:

jquery(customfields[i]).keyup(function(){     calculatesum.call(this); }); 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -