943,678 Members | Top Members by Rank

Ad:
Aug 2nd, 2009
0

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

Expand Post »
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 .....

JavaScript Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
jyotiu is offline Offline
33 posts
since Sep 2008
Aug 3rd, 2009
0

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

I hope this will help you:

html Syntax (Toggle Plain Text)
  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.
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006
Aug 3rd, 2009
0

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

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.
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: generating a table structure using JQuery
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: How to add a "Bookmark This" link?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC