Thursday, 3 September 2015

create dynamic dropdown list with time interval

 function populateList(aList, aStartHour, aEndHour) {  
   var itemIndex = 0;  
   // firstly empty the list if it's got anthing in it.  
   while (aList.options.length > 0) {  
    aList.options.remove(0);  
   }  
   // now add the new items;  
   for (var h = aStartHour; (h < aEndHour); ++h) {  
    var hs = ((h < 10) ? "0" : "") + h;  
    // do the fifteen minute thing  
    for (var m = 0; (m < 60); m += 5) {  
      var opt = document.createElement("OPTION");  
      var ms = ((m < 10) ? "0" : "") + m;  
      aList.options.add(opt);  
      opt.value = "time" + itemIndex;  
      opt.innerText = hs + ":" + ms;  
      ++itemIndex;  
    }  
   }  
   return;  
 } 

Example

populateList(document.getElementById("hlist"), 8, 12);