2

i ve a function which is responsible for some validations, disabling and enabling of some text boxes and a group of radio buttons ....

now i want to generate the function name to be called from another function using a for loop and then appending the for loop index to the function in question ....

something like this .....

function unCheckRadio(num) 
{
  var cont = num;
  var form = document.angular;

  for (var i = 0; i < cont; i++) 
  {
     alert(form['lim_set'+i].length);
     for(var j = 0; j < form['lim_set'+i].length; j++ )
     {
       form['lim_set'+i][j].checked = form['lim_set'+i][j].defaultChecked;
     }
     makeChoice_ang();
  }

}

here i want to append the index i to the makeChoice_ang() function ....

tried many ways .... but no go ...

i tried using this .... 'makeChoice_ang'+i;

but this is making it an string ....

Please help me out or atleast point me in the right direction ....

Thanks a million in advance !!

chinmay
  • 777
  • 2
  • 7
  • 8

5 Answers5

5

I'm adding this as a different answer because, well, it's different!

If you want to call a global function (or any function you know the scope of) given a suffix like that, then you could just use array notation:

window['makeChoice_ang' + i]();

Remember... eval is evil mostly

Community
  • 1
  • 1
nickf
  • 537,072
  • 198
  • 649
  • 721
  • Thanks a heap nick .... worked like a charm ... thanks to all others too ... i couldn't use arrays thats why i was in this fix ... i should have mentioned that ... Thanks a ton to stack overflow – chinmay Aug 03 '09 at 17:34
3

It seems like it would be easier to re-do makeChoice_ang() as a generic function that accepts an index as a parameter. Alternatively, if you can't change the function or if the behavior will vary wildly from function to function you could just use eval() to evaluate your string.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
2

Make an object to put your functions into. Make a method in this object for each 'makeChoice_ang'+i function. You can then call these functions through that object.

var f = {
   makeChoice_ang1: function(){
      alert('1');
   },
   makeChoice_ang2: function(){
      alert('2');
   }
}

for(var i = 1; i < 3;i++)
   f['makeChoice_ang' + i]();
Michael
  • 276
  • 2
  • 2
1

If I understand correctly what you're trying to do:

eval('makeChoice_ang'+i+'()');

This will call the function created by that string concatenation. If i = 5, then it would call makeChoice_ang5().

James Skidmore
  • 49,340
  • 32
  • 108
  • 136
0

In javascript, functions are treated as normal variables, just like a string or an int. obviously they have some extra features, but you can use them in the same way, for example, you could store a number of functions in an array, or pass a function to another function. Given that, I can't see any need to name functions in the way it looks like you're trying to.

I wish I could give you some more help for your specific problem, but it's a little hard to understand, sorry.

You could try changing your unCheckRadio function so that it takes a function as a parameter:

function unCheckRadio(num, func) { ... }

and then you'd call it like this:

function unCheckRadio(5, function() {
    // do whatever here.
});
nickf
  • 537,072
  • 198
  • 649
  • 721