views:

447

answers:

3

I cannot seem to access the context object using a loop context is set: var context = [id1, id2, id3];

This callback function works:

function OnChangeSucceeded(result, context, methodName) {
    document.getElementById(context[0]).disabled = result;
    document.getElementById(context[1]).disabled = result;
    document.getElementById(context[2]).disabled = result;
}

This callback function fails:

function OnChangeSucceeded(result, context, methodName) {
        for(var indx = 0; indx < context.length; indx++) {
           document.getElementById(context[indx]).disabled = result;
        }

    }
A: 

It would be handy to see the calling code so that we could see how your context is established. I'm going to guess that you've set it up as an association and not an array so that when you go to use it in the callback, there is no length property (or it's 0).

When you set it up it should look like:

var context = new Array();
context[0] = 'elem0';
context[1] = 'elem1';
context[2] = 'elem2';

not

var context = { 0: 'elem0', 1: 'elem1', 2: 'elem2' };

If that isn't the problem, then try checking it out in FireFox/FireBug by setting a breakpoint in the onChangeSucceeded function and examining the actual context object to see what properties it has.

tvanfosson
The function calling the PageMethods had the context parameter set as:var context = [id1, id2, id3];I have changed it to:var context = new Array(id1, id2, id3);But still get the same result, I presume the context parameter is passed to the callbacks as somekind of other object.
HadleyHope
A: 

Is it because of your typo?

for(var index = 0; indx < context.length; indx++) {

should be

for(var indx = 0; indx < context.length; indx++) {
meouw
Typo was due to me re-writing the function instead of copying it. Problem still persists.
HadleyHope
A: 

Thats for the pointer to firebug tvanfosson.

I have redone the function and it now works as:

function OnChangeSucceeded(result, context, methodName) {
    for (controlId in context) {
        document.getElementById(context[controlId]).disabled = result;
    }
}

I am not sure if it was because the context was original created as:

context = [id1, id2, id3];

which I have now replaced with:

context = new Array(id1, id2, id3);
HadleyHope