It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

controlling document.write

controlling document.write

Postby dflynn on Tue Apr 14, 2009 1:38 pm

Hey there,
I'm trying to work with the following code:
Code: Select all
funtion setdate (){

var currentTime = new Date();
var year = currentTime.getFullYear();
var hyearb = year-100;

   for($i= hyearb; $i<= year ; $i++) { 
      document.write( "<option value=\"$i\">$i</option>"); 
   } 
}


I need it to write the loop into the page between <select> </select> tags.

I'm sure it's easier than I'm making it, I just don't fully understand javascript yet.
Thanks
User avatar
dflynn
500+ Club
 
Posts: 860
Joined: Wed Oct 03, 2007 9:06 pm
Location: Guelph, Canada

Re: controlling document.write

Postby Suffer on Wed Apr 15, 2009 6:42 am

and were is your <select> </select> tags ?
Suffer
100+ Club
 
Posts: 236
Joined: Tue Jan 20, 2009 6:34 am

Re: controlling document.write

Postby dflynn on Wed Apr 15, 2009 7:20 am

They're further on down the page.
What I need to know is how to call the function without an action, outside of html tags.
I don't think it really matters where the select tags are on the page.
User avatar
dflynn
500+ Club
 
Posts: 860
Joined: Wed Oct 03, 2007 9:06 pm
Location: Guelph, Canada

Re: controlling document.write

Postby dflynn on Wed Apr 15, 2009 11:14 pm

Ok, I have most of it working, figured a lot of it out on my own.

I have this in the header of the page:
Code: Select all
//year
var d = new Date();
var this_year = d.getFullYear();
var cent_year = this_year-100;

//month
var month = new Array();
month[0] = "Jan";
month[1] = "Feb";
month[2] = "Mar";
month[3] = "Apr";
month[4] = "May";
month[5] = "Jun";
month[6] = "Jul";
month[7] = "Aug";
month[8] = "Sep";
month[9] = "Oct";
month[10] = "Nov";
month[11] = "Dec";

//day
function checkData()
{
var yr=document.getElementById("year").value;
var leapyear = 0;
if ((parseInt(yr)%4) == 0)
{
  if (parseInt(yr)%100 == 0)
  {
    if (parseInt(yr)%400 != 0)
    {
    alert("Not Leap");
    leapyear = 0;
    }else{
    alert("Leap Year");
    leapyear = 1;
    }
  }
  if (parseInt(yr)%100 != 0)
  {
    alert("Leap");
    leapyear = 1; 
      }
}
if ((parseInt(yr)%4) != 0)
{
    alert("Not Leap");
    leapyear = 0;
}
}





And this in the body:
Code: Select all


<div>
<label>Bithday:
<label for="year">Year</label>
<select id="year" onChange="checkData()">
<script type="text/javascript">

for(i=this_year; i>=cent_year ; i--) { 
      document.write("<option value=" + i +">" + i +" </option>"); 

   } 

</script>

</select>


<label for="month">Month</label>
<select id="month" name="month" >
<script type="text/javascript">

for(j=0; j<=11 ; j++) { 
      document.write("<option value=" + month[j] +">" + month[j] +" </option>"); 

   } 

</script>
   
</select>


<label for="day">Day</label>
<select id="day">
    <script type="text/javascript">
// I need the code to work here...

</script>
</select>

</div>


So I have it set up so that when the user selects the year they were born, it checks to see that it is or isn't a leapyear.
I'm having trouble figuring out a way to do two things:

1) Depending on the month, the date dropdown will display the correct number of days. Jan 31, Feb 28, March 31... etc

2) If it is a leapyear, I need Feb to have 29 days instead of 28.

Any tips to get this to work?
I've been stuck for a few hours now.

Thanks :D
User avatar
dflynn
500+ Club
 
Posts: 860
Joined: Wed Oct 03, 2007 9:06 pm
Location: Guelph, Canada


Who is online

Users browsing this forum: No registered users and 8 guests