• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 947
  • Last Modified:

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
0
sateeshkumarj
Asked:
sateeshkumarj
1 Solution
 
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now