Passing Array Values Into A Function
In the script below, the console.log is returning the correct values, and both alerts fire. However, the value is coming up undefined. Any ideas what I'm doing wrong? jQuery(docume
Solution 1:
Use Function.prototype.call():
Calls a function with a given
thisvalue and arguments provided individually.
Its first parameter is thisArg:
The value of
thisprovided for the call to fun. Note thatthismay not be the actual value seen by the method: if the method is a function in non-strict mode code, null and undefined will be replaced with the global object, and primitive values will be boxed.
So, pass this from your handler function to calculateSum like so:
jQuery(customfields[i]).keyup(function(){
calculateSum.call(this);
});
Solution 2:
@canon is absolutely correct, following as he said will resolve your Issue.
You can possibly write :-
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++) {
jQuery(customfields[i]).keyup(function () {
calculateSum(this);//Edited
});
}
functioncalculateSum(param) {//Editedvar sum = 0;
alert('value: ' + param.value);//UNDEFINEDif (!isNaN(param.value) && param.value.length != 0 && param.id !== "customfield_21070") {//Edited
sum += parseFloat(param.value);
}
jQuery("#customfield_21070").val(sum.toFixed(2));
}
});
This reflects proper value in your alert. **Tested**
Post a Comment for "Passing Array Values Into A Function"