Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Script not doing as expected

    Hi all having a problem with my javascript and I am not getting any errors

    Code:
    <script type="text/javascript">
    			//function disable()
    			//{
    				//document.book.day.disbaled="true";
    			//}
    			function enableOption()
    			{
    				book=document.book; //Temp hold of form
    				year=book.year.value; //gets year value
    				//year1=parseInt('year');
    				month=book.month.value; //gets month value
    				//month1=parseInt('month');
    				book.day.disabled="true"; //gets day value
    				if (month==="")
    				{
    					//day.disabled="true";
    				}
    				 if ((year = 2012 || 2016 || 2020) && (month= 02))//check for leap year
    				{
    					//day.disabled="false";
    					document.book.day.options.length=0;//clears options
    					//below is populating the option menu
    					document.book.day.options[0]=new Option ("Day","",true,false);
    					document.book.day.options[1]=new Option ("01","01",false,false);
    				}
    			}
    	</script>
    Ok whats happening is I want the thing to check the year and month for matches and then populate the day all using drop down boxes.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    You'll have to dig a little bit more into basics, I presume

    Compare operator is '=='

    The OR operator '||' has to be used with full expressions and not only values

    Code:
    if ((year == 2012 || year == 2016 || year == 2020) && (month == 2))
    on top of that you will have to use parseInt() to make sure that you convert the strings of the input fields into valid integers for comparison.

    So still a lot to do here ...

  • #3
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the quick response

  • #4
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok after reading the excellent advice by devnull69 i went back to the drawing bored and got my script workingish.

    I couldnt get it to work using a if statement so went crazy and went to the switch method
    heres the code
    Code:
    <script type="text/javascript">
    			var arr;
    			var option;
    			function enableOption(val)
    			{
    				var year = document.book.year.value;
    				if ((year==2012)||(year==2016)|| (year==2020))
    					{
    					var day = document.getElementById("day")
    					switch (val)
    						{
    							case "01": case"03": case "05": case"07": case"08": case"10": case"12":
    							arr = new Array("01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31");
    							day.disabled = false;
    							for (var i=0;i<arr.length;i++ )
    							{
    								option = new Option(arr[i],arr[i]);
    								day.options[i] = option;
    							}
    							break;
    
    							case "02":
    							arr = new Array("01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29");
    							day.disabled = false;
    							for (var i=0;i<arr.length;i++ )
    							{
    								option = new Option(arr[i],arr[i]);
    								day.options[i] = option;
    							}
    							break;
    				
    							case "04": case "06": case "09": case "10":
    							arr = new Array("01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30");
    							day.disabled = false;
    							for (var i=0;i<arr.length;i++ )
    							{
    								option = new Option(arr[i],arr[i]);
    								day.options[i] = option;
    							}
    							break;
    
    							default:
    							day.disabled = false;
    							day.options.length =0;
    							break;
    						}
    					}
    					else
    					{
    					var day = document.getElementById("day")
    					switch (val)
    						{
    							case "01": case"03": case "05": case "07": case "08": case "10": case"12":
    							arr = new Array("01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31");
    							day.disabled = false;
    							for (var i=0;i<arr.length;i++ )
    							{
    								option = new Option(arr[i],arr[i]);
    								day.options[i] = option;
    							}
    							break;
    
    							case "02":
    							arr = new Array("01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28");
    							day.disabled = false;
    							for (var i=0;i<arr.length;i++ )
    							{
    								option = new Option(arr[i],arr[i]);
    								day.options[i] = option;
    							}
    							break;
    				
    							case "04": case "06": case "09": case "10":
    							arr = new Array("01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30");
    							day.disabled = false;
    							for (var i=0;i<arr.length;i++ )
    							{
    								option = new Option(arr[i],arr[i]);
    								day.options[i] = option;
    							}
    							break;
    
    							default:
    							day.disabled = false;
    							day.options.length =0;
    							break;
    						}
    
    					}
    			}
    	</script>
    Now this populates the box correctly but if I make a mistake and try to go back it wont repopulate the "day" option. Ive used onChange as well as onClick to handle the even neither work.

    Any suggestions

  • #5
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok i went back to the drawing board and came up with this that is actually workingish

    Heres the code.
    Code:
    <script type="text/javascript">
    			var arr;
    			var option;
    			function enableOption(val)
    			{
    				var year = document.book.year.value;
    				if ((year==2012)||(year==2016)|| (year==2020))
    					{
    					var day = document.getElementById("day")
    					switch (val)
    						{
    							case "01": case"03": case "05": case"07": case"08": case"10": case"12":
    							arr = new Array("01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31");
    							day.disabled = false;
    							for (var i=0;i<arr.length;i++ )
    							{
    								option = new Option(arr[i],arr[i]);
    								day.options[i] = option;
    							}
    							break;
    
    							case "02":
    							arr = new Array("01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29");
    							day.disabled = false;
    							for (var i=0;i<arr.length;i++ )
    							{
    								option = new Option(arr[i],arr[i]);
    								day.options[i] = option;
    							}
    							break;
    				
    							case "04": case "06": case "09": case "10":
    							arr = new Array("01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30");
    							day.disabled = false;
    							for (var i=0;i<arr.length;i++ )
    							{
    								option = new Option(arr[i],arr[i]);
    								day.options[i] = option;
    							}
    							break;
    
    							default:
    							day.disabled = false;
    							day.options.length =0;
    							break;
    						}
    					}
    					else
    					{
    					var day = document.getElementById("day")
    					switch (val)
    						{
    							case "01": case"03": case "05": case "07": case "08": case "10": case"12":
    							arr = new Array("01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31");
    							day.disabled = false;
    							for (var i=0;i<arr.length;i++ )
    							{
    								option = new Option(arr[i],arr[i]);
    								day.options[i] = option;
    							}
    							break;
    
    							case "02":
    							arr = new Array("01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28");
    							day.disabled = false;
    							for (var i=0;i<arr.length;i++ )
    							{
    								option = new Option(arr[i],arr[i]);
    								day.options[i] = option;
    							}
    							break;
    				
    							case "04": case "06": case "09": case "10":
    							arr = new Array("01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30");
    							day.disabled = false;
    							for (var i=0;i<arr.length;i++ )
    							{
    								option = new Option(arr[i],arr[i]);
    								day.options[i] = option;
    							}
    							break;
    
    							default:
    							day.disabled = false;
    							day.options.length =0;
    							break;
    						}
    
    					}
    			}
    	</script>
    Now the problem is if the user makes a mistake it wont change back it if case 01 is chosen then case 02 it dosent change. Used onClick and onChange to no avail.

    Any Suggestions


  •  

    Posting Permissions

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