Thursday, July 29, 2010

Remove Items in a Select Box, IE compliant

function showAllDates() {

var findSel = new Object();
findSel = document.getElementById("idHAllDates")

if (findSel) {
if (findSel.value != 1){
RemoveDates();
}
}
else {
// default
RemoveDates();
}
}

function RemoveDates(){
var nowDate = new Date();
var nowDate2 = new Date(nowDate.getYear(), nowDate.getMonth(), nowDate.getDate());
var findSel = document.getElementById("idSelPEDate");
var tempString = "";

if (findSel)
{

// This does not work in IE
// for (var i = 1; i < findSel.options.length; i++) {

// leave the first option empty
for (var i = findSel.options.length - 1; i >=1; i--) {

// yy/mm/dd : input is in the form of mm/dd/yy
tempString = findSel.options[i].value;
tempString = tempString.substring(0, 2) + "/"
+ tempString.substring(3, 5) + "/"
+ "20" + tempString.substring(6, 8);

var compDate = new Date(tempString);
// alert(nowDate2 + " " + compDate + " " + tempString + " " + findSel.options[i].value);

if (compDate > nowDate2){
alert("bigger: " + compDate);
findSel.remove(i);
}
}
}
}

No comments: