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
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") == "")
....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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