/    Sign up×
Community /Pin to ProfileBookmark

Deleting and adding items to a dropdown box.

hey,

I have the following function.

[code]
function changeDays(months){
alert(“test1”)
itemSelected = document.daysForm.days.selectedIndex;
alert(“test”)
while(itemSelected >= 0){
itemSelected = document.daysForm.days.selectedIndex;
document.daysForm.days.options[itemSelected] = null;
}

// days of each month in an array
var arrDays=new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
nrDays = arrDays[months];

// Add all daynumbers of the selected month to a dropdownbox.
for(x=1; x <= (nrDays – 1); x++){
addOption = new Option(x,x);
document.daysForm.days.options[x] = addOption;
}
}
[/code]

The function is called with the onChange event from a dropdownbox with all the months in it. I want this function to delete all the options in the “days” dropdownbox and add an amount of new options depending on what month is passed to the function.

However, when the function is called, i do get the first alert box saying: “test1”, but not the second alert box.

The “days” dropdown box is written to the page like this:

[code]
document.write(“<form name=’daysForm’ id=’daysForm’>”);
document.write(“<select name=’days’ id=’days’>”);
[/code]

The function call:

[code]
document.write(“<select name=’months’ onChange=’changeDays(this.value)’>”);
[/code]

I hope someone can tell me the problem.

Ps. i’d like to keep the script the same as much as possible.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@NayiasauthorMar 22.2005 — Heres the complete script:
<i>
</i>nrDays = 31;

function writeYears(){
currentDate = new Date();
currentYear = currentDate.getFullYear(); <br/>
addYear = currentYear + 1;

document.write("&lt;form name='yearsForm'&gt;"); <br/>
document.write("&lt;select name='years'&gt;");

for(x=10; x&gt;0; x--){
document.write("&lt;option value='y" + addYear + "'&gt;" + addYear + "&lt;/option&gt;");
addYear = addYear - 1;
}
document.write("&lt;/select&gt;");
document.write("&lt;/form&gt;");
}





function writeMonths(){
document.write("&lt;form name='monthsForm'&gt;");
document.write("&lt;select name='months' onChange='changeDays(this.value)'&gt;");
document.write("&lt;option value='1'&gt;January&lt;/option&gt;");
document.write("&lt;option value='2'&gt;February&lt;/option&gt;");
document.write("&lt;option value='3'&gt;March&lt;/option&gt;");
document.write("&lt;option value='4'&gt;April&lt;/option&gt;");
document.write("&lt;option value='5'&gt;May&lt;/option&gt;");
document.write("&lt;option value='6'&gt;June&lt;/option&gt;");
document.write("&lt;option value='7'&gt;July&lt;/option&gt;");
document.write("&lt;option value='8'&gt;August&lt;/option&gt;");
document.write("&lt;option value='9'&gt;Septembre&lt;/option&gt;");
document.write("&lt;option value='10'&gt;Octobre&lt;/option&gt;");
document.write("&lt;option value='11'&gt;Novembre&lt;/option&gt;");
document.write("&lt;option value='12'&gt;December&lt;/option&gt;");
document.write("&lt;/select&gt;");
document.write("&lt;/form&gt;");
}






function writeDays(nrofdays){
document.write("&lt;form name='daysForm' id='daysForm'&gt;"); <br/>
document.write("&lt;select name='days' id='days'&gt;"); <br/>
for(x = 1; x&lt;=nrofdays; x++){
document.write("&lt;option value='" + x + "'&gt;" + x + "&lt;/option&gt;")
}
document.write("&lt;/select&gt;");
document.write("&lt;/form&gt;");
}





document.write("&lt;table&gt;&lt;tr&gt;&lt;td&gt;");
writeDays(nrDays);
document.write("&lt;/td&gt;&lt;td&gt;");
writeMonths();
document.write("&lt;/td&gt;&lt;td&gt;");
writeYears();
document.write("&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;");






function changeDays(months){
alert("function called")
itemSelected = document.daysForm.days.selectedIndex;
alert("function called2")

while(itemSelected &gt;= 0){
itemSelected = document.daysForm.days.selectedIndex;
document.daysForm.days.options[itemSelected] = null;
}


// days of each month in an array
var arrDays=new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
nrDays = arrDays[months];

// Add all daynumbers of the selected month to a dropdownbox.
for(x=1; x &lt;= (nrDays - 1); x++){
addOption = new Option(x,x);
document.daysForm.days.options[x] = addOption;
}
}


Copy linkTweet thisAlerts:
@ccoderMar 22.2005 — I pulled this from a web page that I did a few years ago. It should give you something to work with.
<i>
</i> function populate(objForm)
{

<i> </i> timeA = new Date(objForm.selYear.options[objForm.selYear.selectedIndex].text,
<i> </i> objForm.selMonth.options[objForm.selMonth.selectedIndex].value, 1);
<i> </i> timeDifference = timeA - 86400000;
<i> </i> timeB = new Date(timeDifference);
<i> </i> var daysInMonth = timeB.getDate();

<i> </i> for (var i = 0; i &lt; objForm.selDay.length; i++)
<i> </i> {
<i> </i> objForm.selDay.options[0] = null;
<i> </i> }
<i> </i> for (var i = 0; i &lt; daysInMonth; i++)
<i> </i> {
<i> </i> if (i &lt; 9)
<i> </i> {
<i> </i> objForm.selDay.options[i] = new Option("0" + (i+1));
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> objForm.selDay.options[i] = new Option(i+1);
<i> </i> }
<i> </i> }
<i> </i> objForm.selDay.options[0].selected = true;

<i> </i> } // end of populate

and
<i>
</i> &lt;select name="selYear" class="droplist"
onchange="populate(this.form);"&gt;
&lt;script type="text/javascript"&gt;
// build the year list - we only need to see past 2 years
timeC = new Date();
currYear = timeC.getFullYear() - 2;

<i> </i> for (var i = 0; i &lt; 3; i++)
<i> </i> {
<i> </i> frmMain.selYear.options[i] = new Option(currYear++);
<i> </i> }
<i> </i> frmMain.selYear.options[2].selected = true;
<i> </i> &lt;/script&gt;
<i> </i> &lt;/select&gt;&amp;nbsp;&amp;nbsp
<i> </i> &lt;select name="selMonth" class="droplist"
<i> </i> onchange="populate(this.form);"&gt;
<i> </i> &lt;option value="01"&gt;Jan&lt;/option&gt;
<i> </i> &lt;option value="02"&gt;Feb&lt;/option&gt;
<i> </i> &lt;option value="03"&gt;Mar&lt;/option&gt;
<i> </i> &lt;option value="04"&gt;Apr&lt;/option&gt;
<i> </i> &lt;option value="05"&gt;May&lt;/option&gt;
<i> </i> &lt;option value="06"&gt;Jun&lt;/option&gt;
<i> </i> &lt;option value="07"&gt;Jul&lt;/option&gt;
<i> </i> &lt;option value="08"&gt;Aug&lt;/option&gt;
<i> </i> &lt;option value="09"&gt;Sep&lt;/option&gt;
<i> </i> &lt;option value="10"&gt;Oct&lt;/option&gt;
<i> </i> &lt;option value="11"&gt;Nov&lt;/option&gt;
<i> </i> &lt;option value="12"&gt;Dec&lt;/option&gt;
<i> </i> &lt;script type="text/javascript"&gt;
<i> </i> // select the current month
<i> </i> currmonth = timeC.getMonth();
<i> </i> frmMain.selMonth.options[currmonth].selected = true;
<i> </i> &lt;/script&gt;
<i> </i> &lt;/select&gt;&amp;nbsp;&amp;nbsp
<i> </i> &lt;select name="selDay" class="droplist"&gt;
<i> </i> &lt;option&gt; &lt;/option&gt;
<i> </i> &lt;option&gt; &lt;/option&gt;
<i> </i> &lt;script type="text/javascript"&gt;
<i> </i> // build the day list
<i> </i> currday = timeC.getDate() - 1;
<i> </i> populate(document.frmMain);
<i> </i> frmMain.selDay.options[currday].selected = true;
<i> </i> &lt;/script&gt;
<i> </i> &lt;/select&gt;
Copy linkTweet thisAlerts:
@NayiasauthorMar 22.2005 — Thanks for the reply, though this is not really what im looking for.

I'd like to know whats wrong with the current script/function that i use.

Why only the first alert box shows up?

I know that the amount of days in february is different once in 4 years, but at the moment im trying to make a simple script to fill a listbox with the amount of days of the selected month. If possible i might adjust the script later.

thx
Copy linkTweet thisAlerts:
@Warren86Mar 23.2005 — Nayias:

I'm not going to debug your code. The following code does what you are attempting to achieve. For what it's worth...

<HTML>

<Head>

<Script Language=JavaScript>

var nStart = 0;

function setDateLimit(isForm,isList,nDays){

prevLimit = isForm[isList].length;
currLimit = nDays;
if (currLimit < prevLimit)
{isForm[isList].length = currLimit}
else {
nOptions = currLimit-prevLimit
for (i=1; i<nOptions+1; i++)
{
isText = (prevLimit+i).toString();
insertDates(isForm[isList],isText,isText);
}
}
isForm[isList].selectedIndex = 0;
checkLeap(isForm,isList)
}

function insertDates(isList,isValue,isText){

isData = new Option(isText,isValue);
isList.add(isData,isList.options.length);
}

function checkLeap(isForm,isList){

isYear = isForm.startYears.value;
if (isYear % 4 == 0 && isForm['startMonths'].value == '28')
{
isData = new Option(29,29)
isForm[isList].add(isData,isForm[isList].options.length)
}
}

function resetBoth(isForm,isMonths,isDates){

isForm[isMonths].selectedIndex = 0;
isForm[isDates].options.length = 0;
for (i=1; i<32; i++)
{
insertDates(isForm[isDates],i,i)
}
}

function clipDates(isForm,isMonth,isList){

nDates = isForm[isMonth].value;
if (nDates == '30')
{isForm[isList].options.length = 30}
if (nDates == '28')
{isForm[isList].options.length = 28}
if (isForm.startYears.value % 4 == 0 && isForm[isMonth].value == '28')
{
isData = new Option(29,29)
isForm[isList].add(isData,isForm[isList].options.length)
}
}

function init(){

isToday = new Date();
nStart = isToday.getMonth()+1+"/"+isToday.getDate()+"/"+isToday.getFullYear();
isStart = nStart.split("/");
listSet.startMonths.selectedIndex = isStart[0]-1;
listSet.startDates.selectedIndex = isStart[1]-1;
yearOffset = isStart[2]-2004;
listSet.startYears.selectedIndex = yearOffset;
clipDates(listSet,'startMonths','startDates')
}

function validate(isForm){

isValid = true;
startStr = isForm.startMonths.selectedIndex+1+"/";
startStr += isForm.startDates.value+"/";
startStr += isForm.startYears.value;
alert(startStr)
}

window.onload=init;


</Script>

</Head>

<Body>

<br>

<form name='listSet' method='post' action=''>

<Table align='center' cellspacing='0'cellpadding='5' style='font-size:14pt;border:solid black 1px;background-color:lightyellow'>

<Thead><TH colspan='4' bgcolor='lightblue'> Choose a Date </TH></Thead>

<TD><select name='startMonths' onchange="setDateLimit(this.form,'startDates',this.value)">

<option selected value='31'> January </option>

<option value='28'> February </option>

<option value='31'> March </option>

<option value='30'> April </option>

<option value='31'> May </option>

<option value='30'> June </option>

<option value='31'> July </option>

<option value='31'> August </option>

<option value='30'> September </option>

<option value='31'> October </option>

<option value='30'> November </option>

<option value='31'> December </option>

</select></TD>

<TD><select name='startDates'>

<option selected value='1'> 1 </option>

<option value='2'> 2 </option>

<option value='3'> 3 </option>

<option value='4'> 4 </option>

<option value='5'> 5 </option>

<option value='6'> 6 </option>

<option value='7'> 7 </option>

<option value='8'> 8 </option>

<option value='9'> 9 </option>

<option value='10'> 10 </option>

<option value='11'> 11 </option>

<option value='12'> 12 </option>

<option value='13'> 13 </option>

<option value='14'> 14 </option>

<option value='15'> 15 </option>

<option value='16'> 16 </option>

<option value='17'> 17 </option>

<option value='18'> 18 </option>

<option value='19'> 19 </option>

<option value='20'> 20 </option>

<option value='21'> 21 </option>

<option value='22'> 22 </option>

<option value='23'> 23 </option>

<option value='24'> 24 </option>

<option value='25'> 25 </option>

<option value='26'> 26 </option>

<option value='27'> 27 </option>

<option value='28'> 28 </option>

<option value='29'> 29 </option>

<option value='30'> 30 </option>

<option value='31'> 31 </option>

</select></TD>

<TD><select name='startYears' onChange="resetBoth(this.form,'startMonths','startDates')">

<option selected value='2004'> 2004 </option>

<option value='2005'> 2005 </option>

<option value='2006'> 2006 </option>

<option value='2007'> 2007 </option>

<option value='2008'> 2008 </option>

<option value='2009'> 2009 </option>

</select></TD>

</TR>

<TR>

<TD colspan='4' align='center' style='border-top:solid black 1px;background-color:moccasin'><input type=button value="Validate" onclick="validate(this.form)"></TD>

</TR>

</Table>

<input type=hidden name='startDate'>

</form>

</Body>

</HTML>
×

Success!

Help @Nayias spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...