Results 1 to 3 of 3

Thread: JavaScript If Statement Help [Resolved]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    492

    Resolved JavaScript If Statement Help [Resolved]

    I have the following code that looks at what month was selected and then based on that it populates the day column with either 30 or 31 days. What happends is when i select a month such as November with 30 days the first time it prepops correctly, but then when i select december with 31 days it prepops it correctly but then when i go back and try to select November again it comes up with 31 days the entire time. Can someone look over my code and tell me what i need to fix.

    Thanks
    Code:
    <script language="JavaScript">
    
    function MonthDropdown(box){
    if(box.id="EventMonth" && box.selectedIndex>0){
    document.getElementById('EventDay').disabled=false
        var listArray = new Array("Day","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"); 
        alert(box.selectedIndex);
    	var obj;
    	var j;
    	var k;
    	obj = "";
    	obj = document.getElementById("EventDay"); 
    
    	if ( (box.id="EventMonth") && ((box.selectedIndex == 4) || (box.selectedIndex == 6) 
    		|| (box.selectedIndex == 9) || (box.selectedIndex == 11))){
    	alert("30");
    	obj.options[j]="";
    	for (j=0; j<31; j++) { 
    	
         obj.options[j] = new Option(listArray[j],listArray[j]); 
         }
    	} else
    	 {
    		 alert("31");
    		 obj.options[k]="";
      	 for (k=0; k<32; k++) { 
         obj.options[k] = new Option(listArray[k],listArray[k]); 
         }
    	} 
    	
    
    }
    else{
    document.getElementById('EventDay').disabled=true
    
    }
    
    }
    </script>
    <script language="JavaScript">
    
    function DayDropdown(box){
    if(box.id="EventDay" && box.selectedIndex>0){
    document.getElementById('EventYear').disabled=false
    }
    else{
    document.getElementById('EventYear').disabled=true
    }
    }
    
    </script>
    </head>
    
    <body>
    
    
    
    <SELECT id="EventMonth" NAME="EventMonth" onchange="MonthDropdown(this)">
    <OPTION>Month</OPTION>
    <OPTION>January</OPTION>
    <OPTION>February</OPTION>
    <OPTION>March</OPTION>
    <OPTION>April</OPTION>
    <OPTION>May</OPTION>
    <OPTION>June</OPTION>
    <OPTION>July</OPTION>
    <OPTION>August</OPTION>
    <OPTION>September</OPTION>
    <OPTION>October</OPTION>
    <OPTION>November</OPTION>
    <OPTION>December</OPTION>
    </SELECT>&nbsp;
    <SELECT id="EventDay" NAME="EventDay" onchange="DayDropdown(this)" disabled>
    <OPTION>Day</OPTION>
    </SELECT>&nbsp;
    <SELECT id="EventYear" NAME="EventYear" disabled>
    <OPTION>Year</OPTION>
    <OPTION>2005</OPTION>
    <OPTION>2006</OPTION>
    <OPTION>2007</OPTION>
    </SELECT></CENTER>
    Last edited by mrstuff68; Jul 5th, 2005 at 09:01 AM.

  2. #2
    Fanatic Member ALL's Avatar
    Join Date
    Jul 2004
    Location
    192.168.1.1
    Posts
    711

    Re: JavaScript If Statement Help

    simple fix, you forgot to clear the options:
    HTML Code:
    <head>
    <script language="JavaScript">
    
    function MonthDropdown(box){
    if(box.id="EventMonth" && box.selectedIndex>0){
    document.getElementById('EventDay').disabled=false
        var listArray = new Array("Day","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"); 
        alert(box.selectedIndex);
    	var obj;
    	var j;
    	var k;
    	obj = "";
    	obj = document.getElementById("EventDay"); 
    	obj.options.length=0
    	if ( (box.id="EventMonth") && ((box.selectedIndex == 4) || (box.selectedIndex == 6) 
    		|| (box.selectedIndex == 9) || (box.selectedIndex == 11))){
    	alert("30");
    	obj.options[j]="";
    	for (j=0; j<31; j++) { 
    	
         obj.options[j] = new Option(listArray[j],listArray[j]); 
         }
    	} else
    	 {
    		 alert("31");
    		 obj.options[k]="";
      	 for (k=0; k<32; k++) { 
         obj.options[k] = new Option(listArray[k],listArray[k]); 
         }
    	} 
    	
    
    }
    else{
    document.getElementById('EventDay').disabled=true
    
    }
    
    }
    </script>
    <script language="JavaScript">
    
    function DayDropdown(box){
    if(box.id="EventDay" && box.selectedIndex>0){
    document.getElementById('EventYear').disabled=false
    }
    else{
    document.getElementById('EventYear').disabled=true
    }
    }
    
    </script>
    </head>
    
    <body>
    
    
    
    <SELECT id="EventMonth" NAME="EventMonth" onchange="MonthDropdown(this)">
    <OPTION>Month</OPTION>
    <OPTION>January</OPTION>
    <OPTION>February</OPTION>
    <OPTION>March</OPTION>
    <OPTION>April</OPTION>
    <OPTION>May</OPTION>
    <OPTION>June</OPTION>
    <OPTION>July</OPTION>
    <OPTION>August</OPTION>
    <OPTION>September</OPTION>
    <OPTION>October</OPTION>
    <OPTION>November</OPTION>
    <OPTION>December</OPTION>
    </SELECT>&nbsp;
    <SELECT id="EventDay" NAME="EventDay" onchange="DayDropdown(this)" disabled>
    <OPTION>Day</OPTION>
    </SELECT>&nbsp;
    <SELECT id="EventYear" NAME="EventYear" disabled>
    <OPTION>Year</OPTION>
    <OPTION>2005</OPTION>
    <OPTION>2006</OPTION>
    <OPTION>2007</OPTION>
    </SELECT></CENTER>
    </body>
    Please support one of my projects?
    TKForums.com

    Web Forum
    JavaScript Wiki
    ________________________
    If somone helps you, please rate their post, by clicking the to rate their post

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    492

    Resolved Re: JavaScript If Statement Help

    Thanks ALL, i knew it was something like that but somehow missed it.

Posting Permissions

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



Click Here to Expand Forum to Full Width