how to append a function name with a foor loop index ??

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Sep 2008
Posts: 33
Reputation: jyotiu is an unknown quantity at this point 
Solved Threads: 0
jyotiu jyotiu is offline Offline
Light Poster

how to append a function name with a for loop index ??

 
0
  #1
Aug 2nd, 2009
Hi all,

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 .....

  1. function unCheckRadio(num)
  2. {
  3. var cont = num;
  4. var form = document.angular;
  5.  
  6. for (var i = 0; i < cont; i++)
  7. {
  8. for(var j = 0; j < form['lim_set'+i].length; j++ )
  9. {
  10. form['lim_set'+i][j].checked = form['lim_set'+i][j].defaultChecked;
  11. }
  12. makeChoice_ang();
  13. }
  14.  
  15. }

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 !!
Last edited by peter_budo; Aug 4th, 2009 at 6:21 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: how to append a function name with a foor loop index ??

 
0
  #2
Aug 3rd, 2009
I hope this will help you:

  1. <html>
  2. <script>
  3. function callAll() {
  4. for(var i = 0; i < 3; i++) {
  5. window['myFunction' + i](i);
  6. }
  7. }
  8. function myFunction0(i) {
  9. alert(i);
  10. }
  11. function myFunction1(i) {
  12. alert(i);
  13. }
  14. function myFunction2(i) {
  15. alert(i);
  16. }
  17. </script>
  18. <body onload='callAll()'>
  19. </body>
  20. </html>

But this is not a good practise.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: how to append a function name with a foor loop index ??

 
0
  #3
Aug 3rd, 2009
Hi,

or you might also want to try this simulated document.
In this demo i will verify all the checkboxes set to have defaulted checked values:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>http://www.daniweb.com</title>
<style type="text/css">
<!--
label { color : #555; font : norm 80%/2 Verdana; letter-spacing : 2px; }
-->
</style>
<script type="text/javascript">
<!--
var unCheckRadio = ( function( num ) { 
var num = num || 0;
var isChecked = { }; // You may also use array in replaced for this object.

var form;
var chb;
   (( form = document.angular ) ? form : document.getElementById( "angular" ));
   for ( i = 0; i < num; i++ ) {
      chb = form[ String( "lim_set" + i ) ];
      chb.checked = chb.defaultChecked;
      if ( chb.defaultChecked ) {
      isChecked[ i ] = chb.id;
/* 
 * Simulating your makeChoice_ang() function, 
   and referencing the index 
   position of the i variable
   via the isChecked = { } object. */
      var makeChoice_ang = ( function( index ) {
         var div;
         (( div = document.getElementById( "output" )) ? div : div =  document.all.output );
         div.innerHTML = "";
         div.innerHTML += "<br><br>This are the following checkboxes the has ( checked by default ) on the page:<br><hr>";

         for ( checkedItem in index ) {
            document.angular[ index[ checkedItem ]].style.border = "2px solid #F00"; 

/* This will highlight the ( checked by default ) checkboxes in your page. */
            div.innerHTML += "<span style=\"color : #365d95; letter-spacing : 2px;\"><b>checkbox #" + (( checkedItem * 1 ) + 1 ) + "</b> — capturing i-index @:<br> position = <b>" + (( checkedItem * 1 ) + 1 ) + "</b></span><br><hr>";
         } delete checkedItem; 
      return;
      } )( isChecked );
      }
   } delete i; 
return false;
} );

// --> 
</script>
</head>
<body>
<div>
<form id="angular" name="angular" action="#" method="post" onsubmit="return unCheckRadio( 4 );">
<div>
<div style="margin-bottom : 1em;"><label for="lim_set0">[ CHECKBox  #1 ] [ <input type="checkbox" id="lim_set0" name="lim_set0" value="0" checked> ]</label><br><br>

<label for="lim_set1">[ CHECKBox  #2 ] [ <input type="checkbox" id="lim_set1" name="lim_set1" value="0"> ]</label><br><br>

<label for="lim_set2">[ CHECKBox  #3 ] [ <input type="checkbox" id="lim_set2" name="lim_set2" value="0" checked> ]</label><br><br>

<label for="lim_set3">[ CHECKBox  #4 ] [ <input type="checkbox" id="lim_set3" name="lim_set3" value="0"> ]</label><br>
</div>
<input type="submit" value="- test function -">
</div>
</form>
<div id="output"></div>
</div>
</body>
</html>

hope we both helped...
-essential
Last edited by essential; Aug 3rd, 2009 at 4:17 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC