Last active
August 29, 2015 14:21
-
-
Save hattmarris/35e316019e51734b60d1 to your computer and use it in GitHub Desktop.
Creates options as 24 hour increments for an HTML select element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Creates options as 24 hour increments for an HTML select element | |
* | |
* @param {string} id: the element id of the select node | |
* @param {string} defaultTime: the default time at hh:mm:ss resolution for the select to display when loaded | |
*/ | |
function createHourSelect(id, defaultTime) { | |
var select = document.getElementById(id); | |
var hours = 24; | |
for(i=0; i < hours; i++) { | |
i = ("0" + i).slice(-2); // convert to two digits if single | |
i = i.toString(); // to string for formatting | |
var value = i + ":00:00"; // timestamp format in value attribute | |
var text = i + ":00"; // format for innerHTML | |
var option = document.createElement("option"); | |
// if this time through loop is equal to defaultTime select the option | |
if(value == defaultTime) { | |
option.setAttribute('selected', 'selected'); | |
} | |
option.setAttribute('value', value); | |
option.innerHTML = text; | |
select.appendChild(option); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment