0

I have the following success call within a jQuery AJAX function.

success: function(data, response) {
            var response1   = data['response1'];
            var response2   = data['response2'];
            var response3   = data['response3'];

            var percentage = window.response / 100;

            alert(percentage);
        }

After the user clicks on a particular button (either response1, response2, or response3) I want to perform some maths with their selected response by working with a dynamic variable window.response.

Unfortunately, I cannot seem to access the value of window.response as alert(percentage) returns 'undefined'. I have tried to access data.response or window['response'] and this doesn't work either.

riverrunner
  • 115
  • 9
  • what about `parseInt(window.response, 10)`? – Suman Bogati Feb 15 '14 at 07:51
  • `console.log(window.response)`?? – Deepak Ingole Feb 15 '14 at 07:55
  • can you show where you defining the `window.response`? – Suman Bogati Feb 15 '14 at 07:59
  • I believe 'window.response' is defended by var response1 = data['response1'] and so forth. The .response part of window.response is dynamic, dependent upon what button the user clicks. Hence it could be window.response1, window.response2, or window.response3. That at least, is my intention. – riverrunner Feb 15 '14 at 08:35
  • Thanks, but I have reviewed the above question and although similar it doesn't resolve my problem. For some reason I simply cannot access the variable window.response or data.response it comes through as undefined. That as I see it is the actual problem here. – riverrunner Feb 15 '14 at 10:15
  • @riverrunner you're defining local variables `response1,response2,response3` but then trying to access a global `response`. That won't work cause even the names are different. – soulcheck Feb 15 '14 at 10:55
  • @soulcheck thanks for your response. I have also tried creating global variables within the function e.g. "response1 = data['response1']" instead of "var response1 = data['response1']". Unfortunately that didn't work either. Regarding the names being different, that is the point: I want to be able to assign the value of percentage to be equal to the value that the user just clicked / 100. I may just resort to messy if statements instead of all this hoo haa. – riverrunner Feb 15 '14 at 11:54

0 Answers0