Jump to content

Indirect variable referencing in Javascript?


jwermont

Recommended Posts

Hello,I would like to know whether Javascript allows a variable name (either as a string, or as the actual address of the variable) to be stored in another variable, and then referenced through the second variable.Say I have a bunch of variables: var0, var1, var2, and so on. I don't know until run-time how many of these variables there will be. That number is stored in "somenum", which could be different each time the page is loaded. I want to be able to indirectly access the contents of each of these variables.Here's a piece of code that might do this, if I had an indirect-reference operator or function:for (i=0; i<somenum; i++) { thisvar = "var" + i alert <indirect-reference operator/function/etc>thisvar)}I haven't been able to find anything that even addresses this issue in either of my Javascript books, which makes me wonder if Javascript even has that capability. But if it does, how does one access the "value of the value" of a variable?Thanks,J

Link to comment
Share on other sites

You can add an ID to each start input(since it's compiled at the server) and then runthrough the form elements to check for values.

<html><head>  <title></title><script type="text/javascript">function ckRadio(form, n){var one_checked = false;var group = form.elements[n];  for(var i = 0; i < group.length; i++){  if(group[i].checked){    one_checked = true;    }  }return one_checked;}function ckForm(){var form = document.forms['f1'];var i = 0;  for(var i = 0; i < form.elements.length; i++){  var field = form.elements[i];  var where = field.id;    if(field.type == 'text'){      if(field.value.length == 0){      alert('Check entries for ' + where + '...');      return false;      }      if(!ckRadio(form, form.elements[i + 1].name)){      alert('Check entries for ' + where + '...');      return false;      }    }    }return true;}</script></head><body><form name="f1" onsubmit="return ckForm(this)"><table><td class="td120">Kathy Jones</td><td class="td60"><input type="text" id="Kathy Jones" name=numgrade0 size="3"></td><td class="td30"><input type="radio" name=grade0 value="A">A</td><td class="td30"><input type="radio" name=grade0 value="B">B</td><td class="td30"><input type="radio" name=grade0 value="C">C</td><td class="td30"><input type="radio" name=grade0 value="D">D</td><td class="td30"><input type="radio" name=grade0 value="F">F</td></tr><tr><td class="td120">Margaret Rogers</td><td class="td60"><input type="text" id="Margaret Rogers" name=numgrade1 size="3"></td><td class="td30"><input type="radio" name=grade1 value="A">A</td><td class="td30"><input type="radio" name=grade1 value="B">B</td><td class="td30"><input type="radio" name=grade1 value="C">C</td><td class="td30"><input type="radio" name=grade1 value="D">D</td><td class="td30"><input type="radio" name=grade1 value="F">F</td></tr><tr><td class="td120">Jack Smith</td><td class="td60"><input type="text" id="Jack Smith" name=numgrade2 size="3"></td><td class="td30"><input type="radio" name=grade2 value="A">A</td><td class="td30"><input type="radio" name=grade2 value="B">B</td><td class="td30"><input type="radio" name=grade2 value="C">C</td><td class="td30"><input type="radio" name=grade2 value="D">D</td><td class="td30"><input type="radio" name=grade2 value="F">F</td></tr></table><input type="submit"></form></body></html>

Post back if you need help with any changes.Thanks,

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...