SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Apr 2004
    Location
    New York
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Mozilla: preset select form options

    Hi - I'm using the following code for an edit form that sets options loaded from the db. Works beautifully in Safari and IE 6, but Mozilla 1.6 ignores it. Any ideas? - Thanks.

    //sTime could = "2:00:00 PM"
    function setWhenTimeOptions(sTime){
    var aHrMin = sTime.split(":");
    var sAMPM = aHrMin[2];
    var aAMPM = sAMPM.split(" ");
    var sHr = aHrMin[0];
    var sMin = ":" + aHrMin[1];
    var sAMPM = " " + aAMPM[1];

    var iHrs = document.addedit.whenHours.length;
    var iMins = document.addedit.whenMinutes.length;
    var iAMPM = document.addedit.whenAMPM.length;

    for ( i = 0; i < iHrs; i++ ){
    var sHrVal = document.addedit.whenHours.options[i].value;
    if (sHr == sHrVal) document.addedit.whenHours.options[i].selected=true;
    }

    for ( j = 0; j < iMins; j++ ){
    var sMinVal = document.addedit.whenMinutes.options[j].value;
    if (sMin == sMinVal) document.addedit.whenMinutes.options[j].selected=true;
    }

    for ( k = 0; k < iAMPM; k++ ){
    var sAMPMVal = document.addedit.whenAMPM.options[k].value;
    if (sAMPM == sAMPMVal) document.addedit.whenAMPM.options[k].selected=true;
    }
    }

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Didn't post the HTML so....not much to see. Here's an alternative:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript">
    
    function setWhenTimeOptions(sTime)
    {
    	var oSelect, oForm = document.forms.addedit;
    	var i, params = sTime.match(/^(\d{1,2})(:\d{2}):\d{2} ((AM)|(PM))$/);
    	if (null != params && params.length >= 4)
    	{
    		oSelect = oForm.elements.whenHours;
    		for ( i = 0; i < oSelect.options.length; i++ )
    			oSelect.options[i].selected = (oSelect.options[i].value == params[1]);
    
    		oSelect = oForm.elements.whenMinutes;
    		for ( i = 0; i < oSelect.options.length; i++ )
    			oSelect.options[i].selected = (oSelect.options[i].value == params[2]);
    
    		oSelect = oForm.elements.whenAMPM;
    		for ( i = 0; i < oSelect.options.length; i++ )
    			oSelect.options[i].selected = (oSelect.options[i].value == params[3]);
    	}
    }
    
    </script>
    </head>
    <body>
    <form name="addedit">
    <select name="whenHours">
    <option value="12" selected="selected">12</option>
    <script type="text/javascript">
    for (var str, n = 1; n < 12; ++n)
    	document.write('<option value="' + n + '">' + n + '</option>');
    </script>
    </select>
    <select name="whenMinutes">
    <script type="text/javascript">
    for (n = 0; n < 60; ++n)
    {
    	str = ((n < 10) ? '0' : '') + n;
    	document.write('<option value=":' + str + '"' + ((n == 0) ? ' selected="selected"' : '') + '>:' + str + '</option>');
    }
    </script>
    </select>
    <select name="whenAMPM">
    <option value="AM">AM</option>
    <option value="PM">PM</option>
    </select>
    </form>
    <br /><br />
    <a href="#" onclick="setWhenTimeOptions('7:09:00 PM');return false;">set to 7:09:00 PM</a>
    <br /><br />
    <a href="#" onclick="setWhenTimeOptions('11:33:42 AM');return false;">set to 11:33:42 AM</a>
    <br /><br />
    <a href="#" onclick="setWhenTimeOptions('1:28:22 PM');return false;">set to 1:28:22 PM</a>
    </body>
    </html>
    Last edited by adios; May 1, 2004 at 10:30.
    ::: certified wild guess :::

  3. #3
    SitePoint Member
    Join Date
    Apr 2004
    Location
    New York
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thanks, adios.

    I never expected a working solution as an answer. It's great! I'm going to have bone up on my regular expressions as your example so elegantly pointed out.

    Since your code fixed the problem I was experiencing with Mozilla browsers, I decided to adapt it to my own a little at a time until I discovered why it worked. The answer was simple, but in my opinion quite arbitrary.

    Mozilla likes to have the object declared early on rather than explicitly called in the code. So where the following code worked:

    var oField = document.formname.element;
    var iLen = oField.length;

    this code didn't:

    var i = document.formname.element.length;

    Thanks again for you help.


Bookmarks

Posting Permissions

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