/    Sign up×
Community /Pin to ProfileBookmark

code not working

Hi,

I’m very very new to javascript.
I tried to write some code, but it doesn’t execute, coould you please have a look at it and tell me where I went wrong?
Also, is there a more efficient way of writing this?

<script type=”text/javascript”>
var d=new Date();
var d1=new Date();
var d2=new Date();
var d3=new Date();
var d4=new Date();
var d5=new Date();

var curMonth=d.getMonth();
var curYear = d.getFullYear();
var date1=new Date();
var date2=new Date();
var date3=new Date();
var date4=new Date();
var date5=new Date();

curYear = curYear.toString().slice(2);

d1.setDate(d1.getDate()+1);
d2.setDate(d2.getDate()+2);
d3.setDate(d3.getDate()+3);
d4.setDate(d4.getDate()+4);
d5.setDate(d5.getDate()+5);

if (d1>30){curMonth=d.getMonth()+1;}
else if (d2>30){curMonth=d.getMonth()+1;}
else if (d3>30){curMonth=d.getMonth()+1;}
else if (d4>30){curMonth=d.getMonth()+1;}
else if (d5>30){curMonth=d.getMonth()+1;}

else curMonth=d.getMonth();

if (curMonth>12){curYear=d.getFullYear()+1;}
else curYear=d.getFullYear();

date1=d1+”-“+curMonth+”-“+curYear;
date2=d2+”-“+curMonth+”-“+curYear;
date3=d3+”-“+curMonth+”-“+curYear;
date4=d4+”-“+curMonth+”-“+curYear;
date5=d5+”-“+curMonth+”-“+curYear;

function getinfo()
{
document.getElementById(“date1”).value=(date1)
document.getElementById(“date2”).value=(date2)
document.getElementById(“date3”).value=(date3)
document.getElementById(“date4”).value=(date4)
document.getElementById(“date5”).value=(date5)

}
</script>

<body onLoad=”getinfo()”>

<form name=”dateForm” id=”dateForm”>
<input type=”text” id=”date1″ name=”date1″ value=”” />
<input type=”text” id=”date2″ name=”date2″ value=”” />
<input type=”text” id=”date3″ name=”date3″ value=”” />
<input type=”text” id=”date4″ name=”date4″ value=”” />
<input type=”text” id=”date5″ name=”date5″ value=”” />

</form>

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@KorSep 20.2010 — Before telling you what's wrong, you would better tell us what the whole code supposes to do.

As a note: Don't give your global variables the same name as the elements' ids or names. IE will misinterpret that.
Copy linkTweet thisAlerts:
@mrmajorauthorSep 20.2010 — Sorry for not explaining.

The code is supposed to calculate a future date (1 to 5 days ahead) and then set the calculated date in a form field, each field a day further ahead.

I want to do this for 25 days eventually, but if I can do it for 5 I can do it for 25 as well.

I'll change the Id's and names then. Does firefox misinterpret them as wel?

Thanks a lot!
Copy linkTweet thisAlerts:
@KorSep 20.2010 — Like this?
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;

&lt;script type="text/javascript"&gt;
onload=function(){
var date=new Date();
var n=5; // how many days ahead?
var D=date.getDate(), dd, mm, yyyy, inp;
var form=document.getElementById('dateForm');
for(var i=0;i&lt;n;i++){
date.setDate(date.getDate()+1);
dd=date.getDate();dd&lt;10?dd='0'+dd:null;
mm=date.getMonth()+1;mm&lt;10?mm='0'+mm:null;
yyyy=date.getFullYear();
inp=document.createElement('input');
inp.type='text';
inp.name='date'+(i+1);
inp.value=dd+'-'+mm+'-'+yyyy;
form.appendChild(inp);
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form id="dateForm" action=""&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@mrmajorauthorSep 20.2010 — Oh my god!

I spent a week trying to figure this out by myself and eventually coming up with the code in the first post.

Now you just hand me the perfect solution in a matter of hours.

This sure opens my eyes to the functionality of javacript.

Thanks for helping me understand this, now I want to go experiment with js ?
×

Success!

Help @mrmajor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...