www.webdeveloper.com
Results 1 to 3 of 3

Thread: Loop in a select form

  1. #1
    Join Date
    Jul 2010
    Posts
    1

    Loop in a select form

    how can i loop the value of the elements in drop down select form?

    its for a date form. i would like to loop 1 - 31 for the date.

  2. #2
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    You need

    for
    new Option

    Hope you can figure it out

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

  3. #3
    Join Date
    Jul 2009
    Posts
    184

    Cool

    Here is a simple example of how to use a for loop to generate <option>'s for a select menu. This example also shows how to make a default <option> be selected:
    Code:
    <script type="text/javascript">
    
    document.write('<select id="day">');
    
    // write one selected option
    document.write('<option value="" selected="selected">Day</option>');
    
    // var num=1, as long as num is less than or equal to 31, num will increment
    for(var num=1; num<=31; num++)
    {
    // write 31 options, with num as the value and text displayed. 
    // should add name attribute for server side
    document.write('<option value="' + num + '">' + num + '</option>');
    }
    
    document.write('</select>');
    
    </script>
    Just a thought: it seems to me you might want to do this with a server side language like php, if you are using php for the form already? That way a user can turn off Javascript in their browser, and still see the generated form instead of pretty much nothing.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles