views:

29

answers:

1

Hello all,

I have an array of div names, the div of which I would like to switch off on loading.

I read from both prototype api and another website that invoke is generally preferred if one is to do the same thing to each of the items in a list.

//I have this switch_off function

function switch_off(div){
  Effect.SwitchOff(div);
}

//and this array
div_names = ['notice','status_bar','word_count']

//Please do tell me if this is not the best option:
div_names.invoke('switch_off');

But it doesn't work.

Is there another parameter I need to supply to invoke? Could it be this?

Added: Here's the Firebug output

//value[method] is undefined
//[Break on this error] return value[method].apply(value, args); 

Thank You!

A: 

The effect of invoke is to call a member method on each of the items in the enumerable and as such is not suited for the type of result you are trying to achieve unless you want to extend the prototype of string.

Something that should provide to be working would be any of the following:

function switch_off(div){
  Effect.SwitchOff(div);
}    

//and this array
div_names = ['notice','status_bar','word_count'];

//Please do tell me if this is not the best option:
div_names.each(switch_off);

or

['notice','status_bar','word_count'].each(function (div){
    Effect.SwitchOff(div);
});
Quintin Robinson
Thanks Quintin, It didn't do the alert. I included Firebug output that might help
Nik
@Nik wow, it's been a LONG time since I've used prototype and misinterpreted the documentation, the effect of `invoke` is to call a member method on each of the items in the enumerable. So in your instance it will never work. I will update a reasonable solution.
Quintin Robinson
Just curious, what do you use instead of prototype now or do you not program much in js anymore?
Nik
@Nik I do still use javascript, I am a big fan of the language actually. When I use a ready made library my preference is jQuery it's a very elegant library but a departure from prototype for sure.
Quintin Robinson
@Quitin I see. I am thinking about switching, too, as Prototype hasn't been updated for 3 years? until recently. Thanks again!
Nik