PDA

View Full Version : Script not doing as expected



dashwall
01-05-2011, 01:04 PM
Hi all having a problem with my javascript and I am not getting any errors :(



<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.

devnull69
01-05-2011, 01:12 PM
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



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 ...

dashwall
01-05-2011, 01:16 PM
thanks for the quick response

dashwall
01-05-2011, 03:11 PM
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


<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

dashwall
01-05-2011, 03:15 PM
ok i went back to the drawing board and came up with this that is actually workingish

Heres the 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