Jump to content

how 2 access form's components with looping


dhie_20

Recommended Posts

Hi, I'm a new comer here.I would like to ask, how to access form's components within looping.supposed I have 10 Text Input Type in a form, and I want to change the value of TEXT Type components using Javascript like this:document.form1.text_1.value="hello";document.form1.text_2.value="hello";document.form1.text_3.value="hello";...... until text_10I had tried to optimize the code by using the "for" loop like this, but it didn't work at all :) :for (i=1;i>=10;i++) { document.form1.text_+i+.value="hello"; }does anyone know how to solve this problem by using javascript? I need this trick so much :) . Thank You so much guys :)

Link to comment
Share on other sites

for (i=1;i>=10;i++) {  document.form1.text_+i+.value="hello"; }

Your condition statement in your "for" is i>=10 and perhaps should be i<=10. Your code within is never executed. Think of "for" as:loop i (starts at 1) while i is less than or equal to 10, incrementing i by 1 after each loop.WG
Link to comment
Share on other sites

var x;var abc;y=document.getElementsByTagName("input")for(z=0;z<=y.length;z++) {if(y.type=="text" && y.parentNode.name=="form1") {x[zoop]==y;zoop++;}}for (i=0;i<=x.length;i++){x[i].value=="Hello"}

Try that on for size?

Link to comment
Share on other sites

var x;var abc;y=document.getElementsByTagName("input")for(z=0;z<=y.length;z++) {if(y.type=="text" && y.parentNode.name=="form1") {x[zoop]==y;zoop++;}}for (i=0;i<=x.length;i++){x[i].value=="Hello"}

Try that on for size?

oups, I had put a wrong condition for "i". that's correct. the "i" should be less or equal than "10".Mr. Moderator, this is a very interesting code. I see that the first step I have to do is to collect all <INPUT TYPE=TEXT> tag into an array. If I have the array, then I could assign each value one by one. :) very niceBut, I hava a few question :1. I see two method/function used there (getElementsByTagName and parentNode). Where can I get the full list and description of all method in Javascript?2. what is the initial value for "zoop"? Thanks moderator for the codeI'm very appreciate it :) regards,-d20-
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...