Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript beginner please help with this code 1

Status
Not open for further replies.

vaks009

Programmer
Jun 14, 2001
11
CA
Hi:
I am trying to write a javascript using onLoad event on Select object. i want to set the values of months based on following logic
If (current month > month selected)
use current year+1
else
use current year
and then replace current month values with new values. so for example if we select jan, then jan value should be
01/01/2002 and if we select October then value should be
10/01/2001.
i tried using onChange,and onFocus event but want to do with onLoad event so that all the values are set when the screen loads.
thanks in advance,
vaks
Here is what i have tried so far:<!DOCTYPE HTML PUBLIC &quot;-//IETF//DTD HTML//EN&quot;>
<html>

<head>
<meta http-equiv=&quot;Content-Type&quot;
content=&quot;text/html; &quot;>
<meta name=&quot;Author&quot;
content=&quot;All rights reserved.&quot;>
<meta name=&quot;GENERATOR&quot; content=&quot;vi&quot;>
<title>Registration: Class Query</title>


<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

function populate(myForm,myCnt) {
var dateNow = new Date();
var yearNow = dateNow.getFullYear();
var monthNow = dateNow.getMonth();

if (monthNow > myCnt) {
newYear = yearNow+1;
alert(newYear);
}
else {
newYear = yearNow;
}

myval= document.f1.field_value.options[myCnt].value;

if (myval.length != 10) {
document.f1.field_value.options[myCnt].value = (document.f1.field_value.options[myCnt].value+newYear);
}

alert(document.f1.field_value.options[myCnt].value);
document.f1.field_value.options[myCnt].selected=true;
}

</script>

</head>

<body bgcolor=&quot;#FFFFFF&quot;>


<form name=f1>

<table border=&quot;0&quot; cellpadding=&quot;2&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot;>
<tr>
<td valign=&quot;top&quot; width=&quot;100%&quot;><hr>
<h3 align=&quot;center&quot;>Course Query</h3>
<form>
<table border=&quot;0&quot; cellpadding=&quot;4&quot; cellspacing=&quot;0&quot;
width=&quot;100%&quot;>

<tr>
<td><b>Select Date:</b></td>

<td><select name=&quot;field_value&quot; onFocus=&quot;populate(this.form,this.selectedIndex);&quot; onChange=&quot;populate(this.form,this.selectedIndex);&quot; size=&quot;1&quot;>
<option value=&quot;01/01&quot;>Jan</option>
<option value=&quot;02/01&quot;>Feb</option>
<option value=&quot;03/01&quot;>Mar</option>
<option value=&quot;04/01&quot;>Apr</option>
<option value=&quot;05/01&quot;>May</option>
<option value=&quot;06/01&quot;>Jun</option>
<option value=&quot;07/01&quot;>Jul</option>
<option value=&quot;08/01&quot;>Aug</option>
<option value=&quot;09/01&quot;>Sep</option>
<option value=&quot;10/01&quot;>Oct</option>
<option value=&quot;11/01&quot;>Nov</option>
<option value=&quot;12/01&quot;>Dec</option>
</select>
</td>
</tr>


</table>
</form>
</td>
</tr>

</table>
</form>
</body>
</html>


 
hiee!

here is your script
copy-paste it, works in ie5.0 didnt checked in others

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

function populate(myForm,myCnt) {
var dateNow = new Date();
var yearNow = dateNow.getFullYear();
var monthNow = dateNow.getMonth();

if (monthNow > myCnt) {
newYear = yearNow+1;
// alert(newYear);
}
else {
newYear = yearNow;
}

myval= document.f1.field_value.options[myCnt].value;

if (myval.length != 10) {
document.f1.field_value.options[myCnt].value = (document.f1.field_value.options[myCnt].value+newYear);
}

//alert(document.f1.field_value.options[myCnt].value);
if (document.getElementById){
document.getElementById(&quot;neste3&quot;).innerHTML=document.f1.field_value.options[myCnt].value
}
if (document.all){
document.all.neste3.innerHTML=document.f1.field_value.options[myCnt].value
}
//document.f1.field_value.options[myCnt].selected=true;
}
</script>


here is your form: (your prev form had a nested form tag - mistake...)

<form name=f1>
<table border=&quot;0&quot; cellpadding=&quot;2&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot;>
<tr>
<td valign=&quot;top&quot; width=&quot;100%&quot;><hr>
<h3 align=&quot;center&quot;>Course Query</h3>
<table border=&quot;0&quot; cellpadding=&quot;4&quot; cellspacing=&quot;0&quot;
width=&quot;100%&quot;>
<tr>
<td><b>Select Date:</b><ilayer id=neste1><layer id=neste2 width=400><div id=neste3 style='position:relative'></div></layer></ilayer></td>
<td><select name=&quot;field_value&quot; onChange=&quot;populate(this.form,this.selectedIndex);&quot; >
<option value=&quot;01/01&quot; >Jan
<option value=&quot;02/01&quot;>Feb
<option value=&quot;03/01&quot; selected>Mar
<option value=&quot;04/01&quot;>Apr
<option value=&quot;05/01&quot;>May
<option value=&quot;06/01&quot;>Jun
<option value=&quot;07/01&quot;>Jul
<option value=&quot;08/01&quot;>Aug
<option value=&quot;09/01&quot;>Sep
<option value=&quot;10/01&quot;>Oct
<option value=&quot;11/01&quot;>Nov
<option value=&quot;12/01&quot;>Dec
</select>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>


change selected position - it is selected by default option

regards, vic
 
Thanks a lot Vic. That was great help.
Regards,
vaks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top