0

I've seen a bunch of examples that are close, but none that are right on.

I'm building a program that is an HTA form where people can enter multiple lines of similar data (ex. mailing addresses). The number of lines that the user will enter is determined by an input box that receives a number:

<html>
  <head>
    <title>Data Entry Application</title>
    <hta:application id = "ExampleHTA" singleInstance = "yes" icon = "macexp.ico" border = "thin" minimizeButton = "no" maximizeButton = "no" scroll = "yes" />

    <script language="javascript" type="text/javascript">
      function numOfLines(choice)
      {
        for(var i = 0; i < choice; ++i)
        {
          document.getElementById("addy_lines").innerHTML = "<br />Name: <input type='text' id='Name' size=50 maxlength=50 /> Address: <input type='text' id='Address' size=50 maxlength=50 /> City: <input type='text' id='City' size=15 maxlength=15 /> State: <input type='text id='State' size=2 maxlength=2 /> Zip Code: <input type='text' id='Zip' size=5 maxlength=5 />";
        }
      }
    </script>
  </head>
  <body>
    <form>
      Number of Address Lines: <input type="text" id="AddyLines" size=2 maxlength=2 onChange="numOfLines(this.value);">
      <div id="addy_lines"></div>
    </form>
  </body>
</html>

http://jsfiddle.net/isherwood/r9KEH/

When the user enters a number in the AddyLines input box, the same number of lines should appear in the div id="addy_lines" section.

Example: The user enters 5, then five input lines should be created. Currently, only one address input line gets created within the div tags.

1
  • Like Jurik mentioned, you're creating duplicate IDs. Use classes or increment. Commented Apr 23, 2014 at 15:05

2 Answers 2

2

You wrote:

document.getElementById("addy_lines").innerHTML = "<br />Name: <input type='text' id='Name' size=50 maxlength=50 /> Address: <input type='text' id='Address' size=50 maxlength=50 /> City: <input type='text' id='City' size=15 maxlength=15 /> State: <input type='text id='State' size=2 maxlength=2 /> Zip Code: <input type='text' id='Zip' size=5 maxlength=5 />";

but you need to add the string, not overwrite the innerHTML:

document.getElementById("addy_lines").innerHTML+= '<br/>' +
     'Name: <input type="text" id="Name" size="50" maxlength="50" /> ' +
     'Address: <input type="text" id="Address" size="50" maxlength="50" /> ' +
     'City: <input type="text" id="City" size="15" maxlength="15" /> ' +
     'State: <input type="text" id="State" size="2" maxlength="2" /> ' +
     'Zip Code: <input type="text" id="Zip" size="5" maxlength="5" />';

But overall it is a bad solution, because now you have several input fields with same id but id should be unique.

So this might be much better:

document.getElementById("addy_lines").innerHTML+= '<br />' +
    'Name: <input type="text" id="Name-' + i + '" size="50" maxlength="50" /> ' +
    'Address: <input type="text" id="Address-' + i + '" size="50" maxlength="50" /> ' +
    'City: <input type="text" id="City-' + i + '" size="15" maxlength="15" /> ' +
    'State: <input type="text" id="State-' + i + '" size="2" maxlength="2" /> ' +
    'Zip Code: <input type="text" id="Zip-' + i + '" size="5" maxlength="5" />';

Further I would give all elements a name and use classes instead of id to target them better with jQuery. But that is up to you.

Sign up to request clarification or add additional context in comments.

1 Comment

This solution worked: document.getElementById("addy_lines").innerHTML+=.... I will look into giving the elements their own name. Thanks. :)
0

How about changing

document.getElementById("addy_lines").innerHTML = "<br />Name: <input type='text' id='Name' size=50 maxlength=50 /> Address: <input type='text' id='Address' size=50 maxlength=50 /> City: <input type='text' id='City' size=15 maxlength=15 /> State: <input type='text id='State' size=2 maxlength=2 /> Zip Code: <input type='text' id='Zip' size=5 maxlength=5 />";

to

document.getElementById("addy_lines").innerHTML = document.getElementById("addy_lines").innerHTML + "<br />Name: <input type='text' id='Name' size=50 maxlength=50 /> Address: <input type='text' id='Address' size=50 maxlength=50 /> City: <input type='text' id='City' size=15 maxlength=15 /> State: <input type='text id='State' size=2 maxlength=2 /> Zip Code: <input type='text' id='Zip' size=5 maxlength=5 />";

You should append the changes and not replace them.

1 Comment

Not that good because now he has same ids and they should be unique.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.