Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Auto Populating a Form Element

    Hello,

    I know you can create a form select element using this method.

    Code:
    <form name="myform">
    <select name="age">
     <option name="1">1</option>
     <option name="2">2</option>
     <option name="3">3</option>
     <option name="4">4</option>
     <option name="5">5</option>
     <option name="6">6</option>
    </select>
    </form>
    I also know that you can create a loop to populate the a list using this function:

    Code:
     for(var count = 1 ; count < 71 ; count++)
      document.write(count + "<br>");
    However, how do I populate a form select tag with a loop script so I don't have to create 70 different options?

    Thank you in advanced for the help.

    Jason H.

  • #2
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    with document.write you can do it this way:
    Code:
    for(var x=1 ; x < 71 ; x++)
        document.write('<option value="' + x + '">' + x);
    as a function:
    Code:
    function write_options(start, end) {
        for( ; start <= end ; start++)
            document.write('<option value="' + start + '">' + start);
    }
    
    document.write('<select>');
    write_options(1, 70);
    document.write('</select>');
    Last edited by fci; 11-18-2005 at 11:40 PM.

  • #3
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Awesome!! This works perfectly. Thank you for the quick reply.

    Jason H.

  • #4
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Just as an alternative you could also do it like this:

    Code:
    <html>
    	<head>
    		<script type="text/javascript">
    			function buildSelect(num)
    			{
    				var sel=document.createElement('select');
    				sel.setAttribute('name','select1');
    				var len=num+1;
    				for(var i=1;i<len;i++)
    				{
    					sel.options[sel.options.length]=new Option(i,i);
    				}
    				document.getElementById('sel_spot').appendChild(sel);
    			}
    		</script>
    	</head>
    	<body onload="buildSelect(70);">
    		<form>
    			<span id="sel_spot"></span>
    		</form>
    	</body>
    </html>
    Basscyst
    Helping to build a bigger box. - Adam Matthews

  • #5
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I did come up with one question though, perhaps someone will know this one.

    In this example:

    Code:
      <script language="javascript" type="text/javascript">
       function write_options(start, end) {
        for( ; start <= end ; start++)
            document.write('<option value="' + start + '">' + start);
        }
            document.write('<select>');
            write_options(1, 70);
            document.write('</select>');
       </script>
    I noticed there is no name for the select element. How do I give this a name so I can post the selection to a database, such as mySQL?

    For example,
    Code:
    <select name="myName"><option name="opt1">opt1</option></select>
    Its the "myName" field that I will be populating to a database.

    Thanks again.

    Jason

  • #6
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Just give it a name as normal in the code:

    Code:
    <script language="javascript" type="text/javascript">
       function write_options(start, end) {
        for( ; start <= end ; start++)
            document.write('<option value="' + start + '">' + start);
        }
            document.write('<select name="mySelectName">');
            write_options(1, 70);
            document.write('</select>');
       </script>
    Basscyst
    Helping to build a bigger box. - Adam Matthews

  • #7
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Basscyst
    Just give it a name as normal in the code:

    Code:
    <script language="javascript" type="text/javascript">
       function write_options(start, end) {
        for( ; start <= end ; start++)
            document.write('<option value="' + start + '">' + start);
        }
            document.write('<select name="mySelectName">');
            write_options(1, 70);
            document.write('</select>');
       </script>
    Basscyst
    Doh! That was easy... I now recognize the pattern. Thanks for all the help!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •