Jump to content

[SOLVED] array string operation/syntax help


poofacer

Recommended Posts

var options = new Array ("option1","option2", "option3","option4","option5", "option6","option7","option8","option9","option10");
  for(var i=0; i<10; i++)
  { if(form.[b]options[i][/b].value == "")
    { ecount++;
    }
  }

 

The above code is javascript.

 

I have 10 input types with the names option1 to option10 which i generated using a for loop with PHP.

My problem is that this part of my code is not working the way it should and the problem is the bold part. I want the for loop to generate those strings in the array in place of "options[i ]". but it doesnt seem to be proper syntax. i am new to js but have done C programming.

 

If i test form.option1.value= "" it works perfectly. But i dont want to do that for all ten options and i am curious about why this doesnt work.

Link to comment
https://forums.phpfreaks.com/topic/50569-solved-array-string-operationsyntax-help/
Share on other sites

if you have ids in your input

<input type="text" name="option1" id="option1" .....

 

You can use the document.getElementById()

...
if(document.getElementById(options[i]).value == "")
....

 

If not, use the eval() function

...
if (eval("form."+options[i]+".value") == "")
....

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.