javascript element search

My page Html page look like this


<html>
<body>
<input type="text" name="txtJpn_0" >
<input type="text" name="txtOthers_0">
<input type="text" name="txtJpn_1">
<input type="text" name="txtOthers_1">
<input type="text" name="txtJpn_2">
<input type="text" name="txtOthers_2">
<input type="text" name="txtJpn_3">
<input type="text" name="txtOthers_3">
<input type="text" name="txtJpn_4">
<input type="text" name="txtOthers_4">
<input type="text" name="txtJpn_5">
<input type="text" name="txtOthers_5">
<input type="text" name="txtJpn_6">
<input type="text" name="txtOthers_6">
</body>
</html>

On windows onload I want to populate txtOthers_X based on txtJpn_x  the problem is text box are dynamic so we dont know exatly how many text box will load so the javascript should also be dynamic by using getelementsbyID or enything like this. can anyone help me
sateeshkumarjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BogoJokerCommented:
Hi sateeshkumarj,

How about this function, it loops through the elements in a the form, you jus need to add <form> after <body> and </form> before </body> and this should work.  The trick is you always have a txtJpn and a txtOthers.  So just loop through, and each time you do two operations.  First I get the value from jpn, then I put the value in others:

function populate()
{
  for (var i = 0; i < documents.forms[0].elements.length; i++)
  {
    var txt = documents.forms[0].elements[i].value;
    i++;
    documents.forms[0].elements[i].value = txt;
  }
}

I hope that works for you,
Joe P
0
sateeshkumarjAuthor Commented:
Hi joe,
 It should be specific in future if another input textbox is added in between or end it will be a problem again and also there are some text box at the end of the page for other input.

0
clockwatcherCommented:
<html>
<head>
<script language="javascript">
function fill()
{
  var re = /^txtJpn_(\d+)/;
  var match;

  for (var i = 0; i < document.forms[0].elements.length; i++)
  {
     match = re.exec(document.forms[0].elements[i].name);
     if (match) {
         document.getElementById("txtOthers_" + match[1]).value = document.forms[0].elements[i].value;
     }
  }
}
</script>
</head>
<body>
<form>
<input type="text" name="txtJpn_0" >
<input type="text" name="txtOthers_0">
<input type="text" name="txtJpn_1">
<input type="text" name="txtOthers_1">
<input type="text" name="txtJpn_2">
<input type="text" name="txtOthers_2">
<input type="text" name="txtJpn_3">
<input type="text" name="txtOthers_3">
<input type="text" name="txtJpn_4">
<input type="text" name="txtOthers_4">
<input type="text" name="txtJpn_5">
<input type="text" name="txtOthers_5">
<input type="text" name="txtJpn_6">
<input type="text" name="txtOthers_6">
<input type="button" onclick="fill();" value="Fill">
</form>
</body>
</html>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.