Results 1 to 3 of 3

Thread: select box default

  1. #1
    Join Date
    Jun 2014

    select box default

    In looking at the code below, I have a select box where the default value is option value = “01” What i would like to do is make the default value (when page loads) default to what the time is set to. For instance if i use this form to set the time to 4AM, I would have the select box default to 4AM when i open the page.

    I have not done any html in a few years and my brain is cooking .
    Im thinking i will need to set a variable for instance (varTim) and set that to pick up from the application when the page loads. Then put an “if” statement in each of the options so the the correct one will be displayed when the form is opened.
    Can anyone help me with how to do this?
    <select name="onTime" id="onTime" style="width:200x ; height: 50px; font-size:36px"></option>
                 <option value="0">Midnight</option>
                <option value="01" selected>1am</option>
                <option value="02">2am</option>
                <option value="03">3am</option>
                <option value="04">4am</option>
                <option value="05">5am</option>
                <option value="06">6am</option>
                <option value="07">7am</option>
                <option value="08">8am</option>
                <option value="09">9am</option>
                <option value="10">10am</option>
                <option value="11">11am</option>
                <option value="12">12am</option>
                <option value="13">1pm</option>
                <option value="14">2pm</option>
                <option value="15">3pm</option>
                <option value="16">4pm</option>
                <option value="17">5pm</option>
                <option value="18">6pm</option>
                <option value="19">7pm</option>
                <option value="20">8pm</option>
                <option value="21">9pm</option>
                <option value="22">10pm</option>
                <option value="23">11pm</option>

  2. #2
    Join Date
    Aug 2004
    Sounds like something you'd do on the server side? For instance, if using PHP:
    PHP Code:
    <option value="04"<?php
    if(!empty($someVariable) and $someVariable == 4) {
    ' selected="selected"';
    (Of course, you'd preferably have that in some sort of loop so that you don't have to repeat that for each increment.)
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Apr 2014
    You could do it client side too using javascript. Inside a page onload function, get the current date time and find the current hour (see this) and use that to pick the value you want in the drop down.


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