0

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(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));
    }

});
2
  • 1
    this does not refer to the keyup'd element in calculateSum. Commented Apr 18, 2013 at 19:46
  • this in the current context it's in in your calculateSum function isn't calling anything. Commented Apr 18, 2013 at 19:47

2 Answers 2

1

Use Function.prototype.call():

Calls a function with a given this value and arguments provided individually.

Its first parameter is thisArg:

The value of this provided for the call to fun. Note that this may 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);
});
Sign up to request clarification or add additional context in comments.

Comments

0

@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
                });
            }


            function calculateSum(param) {//Edited

                var sum = 0;

                alert('value: ' + param.value);//UNDEFINED

                if (!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**

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.