/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Calendar date picker to textarea question

I’m trying to modify a script from the ‘Dynamic Drive’ site that displays a calendar.

I modified it so that when you click on a day, it posts that mm/dd/yyyy format to the adjacent textarea.

Problem is, when I do it again, it appends to the textarea rather than move down to a new line.
I have tried various combinations of ‘n’ in the write to the textarea and the commented-out alert show the correct format.

What do I need to do to start the next date on a new line in the text area?

[code]
<html>
<head>
<style type=”text/css”>

.main {
width:200px;
border:1px solid black;
}

.month {
background-color:black;
font:bold 12px verdana;
color:white;
}

.daysofweek {
background-color:gray;
font:bold 12px verdana;
color:white;
}

.days {
font-size: 12px;
font-family:verdana;
color:black;
background-color: lightyellow;
padding: 2px;
cursor:pointer;
}

.days #today{
font-weight: bold;
color: red;
}
.btnCal {
width:36px;
}

</style>

<script type=”text/javascript”> <!– src=”basiccalendar.js” –>
/***********************************************
* Basic Calendar-By Brian Gosselin at http://scriptasylum.com/bgaudiodr/
* Script featured on Dynamic Drive (http://www.dynamicdrive.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function buildCal(m, y, cM, cH, cDW, cD, brdr){
var mn=[‘January’,’February’,’March’,’April’,’May’,’June’,’July’,’August’,’September’,’October’,’November’,’December’];
var dim=[31,0,31,30,31,30,31,31,30,31,30,31];

var oD = new Date(y, m-1, 1); //DD replaced line to fix date bug when current day is 31st
oD.od=oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st

var todaydate=new Date() //DD added
var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0 //DD added

dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
var t=”;
t += t='<div class=”‘+cM+'”>’;
t += ‘<table class=”‘+cM+'” cols=”7″ cellpadding=”0″ border=”‘+brdr+'” cellspacing=”0″>’;
t += ‘<tr align=”center”>’;
t += ‘<td colspan=”7″ align=”center” class=”‘+cH+'”>’+mn[m-1]+’ – ‘+y+'</td></tr><tr align=”center”>’;
for (s=0;s<7;s++) t+='<td class=”‘+cDW+'”>’+”SMTWTFS”.substr(s,1)+'</td>’;
t+='</tr><tr align=”center”>’;
for(i=1;i<=42;i++){
var x=((i-oD.od>=0)&&(i-oD.od<dim[m-1]))? i-oD.od+1 : ‘&nbsp;’;
var xt = ”; if (x==scanfortoday) xt='<span id=”today”>’+x+'</span>’ //DD added
if (xt == ”) xt = x;

// t+='<td class=”‘+cD+'” onclick=”alert(‘+formatMDY(m,x,y)+’)”>’+xt+'</td>’;
t+='<td class=”‘+cD+'” onclick=”document.getElementById(‘selectedDates’).value+=’+formatMDY(m,x,y)+’n”>’+xt+'</td>’;

if(((i)%7==0)&&(i<36))t+='</tr><tr align=”center”>’;
}
return t+='</tr></table></div>’;
}
function formatMDY(m,x,y) {
var MM = (m<10)?’0’+m:m;
var DD = (x<10)?’0’+x:x;
return “‘”+MM+’/’+DD+’/’+y+”‘”;
}
</script>
</head>
<body>

<div><center>
<table border=’0’><tr><td>
<div id=”SingleCal”></div

<script type=”text/javascript”>
var todaydate = new Date();
var curmonth = 0;
var curyear = 0;

function getNow() {
todaydate=new Date()
curmonth=todaydate.getMonth()+1 //get current month (1-12)
curyear=todaydate.getFullYear() //get current year
}
function ShowMonth(curmonth,curyear) {
document.getElementById(‘SingleCal’).innerHTML = buildCal(curmonth ,curyear, “main”, “month”, “daysofweek”, “days”, 1);
}

getNow(); ShowMonth(curmonth,curyear);

</script>
<button class=”btnCal” onclick=”curyear-=1;ShowMonth(curmonth,curyear)”>&lt;Y</button>
<button class=”btnCal” onclick=”curmonth–;if (curmonth<1) { curmonth=12; };ShowMonth(curmonth,curyear)”>&lt;M</button>
<button onclick=”getNow();ShowMonth(curmonth,curyear)”>Now</button>
<button class=”btnCal” onclick=”curmonth++;if (curmonth>12) { curmonth=1; };ShowMonth(curmonth,curyear)”>M&gt;</button>
<button class=”btnCal” onclick=”curyear+=1;ShowMonth(curmonth,curyear)”>Y&gt;</button>
</center>
</td><td>
<textarea id=”selectedDates” rows=”8″ cols=”16″></textarea><br>
<button onclick=”document.getElementById(‘selectedDates’).value=””>Clear</button>
</td></tr>
</table>
</div>
</body>
</html>
[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@brenz_netFeb 26.2010 — FYI - your original script (and my modified one) wouldn't render the calendar in IE, but they work in FF.

I used all of your original code, I just put the bit that updates the textarea into a function, and it seemed to do the job. Took a while to get to that point tho! :eek:

[CODE]
<script type="text/javascript"> <!-- src="basiccalendar.js" -->
/***********************************************
* Basic Calendar-By Brian Gosselin at http://scriptasylum.com/bgaudiodr/
* Script featured on Dynamic Drive (http://www.dynamicdrive.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function buildCal(m, y, cM, cH, cDW, cD, brdr){
var mn=['January','February','March','April','May','June','July','August','September','October','November','December'];
var dim=[31,0,31,30,31,30,31,31,30,31,30,31];

var oD = new Date(y, m-1, 1); //DD replaced line to fix date bug when current day is 31st
oD.od=oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st

var todaydate=new Date() //DD added
var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0 //DD added

dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
var t='';
t += t='<div class="'+cM+'">';
t += '<table class="'+cM+'" cols="7" cellpadding="0" border="'+brdr+'" cellspacing="0">';
t += '<tr align="center">';
t += '<td colspan="7" align="center" class="'+cH+'">'+mn[m-1]+' - '+y+'</td></tr><tr align="center">';
for (s=0;s<7;s++) t+='<td class="'+cDW+'">'+"SMTWTFS".substr(s,1)+'</td>';
t+='</tr><tr align="center">';
for(i=1;i<=42;i++){
var x=((i-oD.od>=0)&&(i-oD.od<dim[m-1]))? i-oD.od+1 : '&nbsp;';
var xt = ''; if (x==scanfortoday) xt='<span id="today">'+x+'</span>' //DD added
if (xt == '') xt = x;

// t+='<td class="'+cD+'" onclick="alert('+formatMDY(m,x,y)+')">'+xt+'</td>';
//t+='<td class="'+cD+'" onclick="document.getElementById('selectedDates').value+='+formatMDY(m,x,y)+'n">'+xt+'</td>';
t+='<td class="'+cD+'" onclick="updateSelDates('+m+','+x+','+y+');">'+xt+'</td>';

if(((i)%7==0)&&(i<36))t+='</tr><tr align="center">';
}
return t+='</tr></table></div>';
}
function formatMDY(m,x,y) {
var MM = (m<10)?'0'+m:m;
var DD = (x<10)?'0'+x:x;
return MM+'/'+DD+'/'+y;
}
function updateSelDates(m1,x1,y1) {
document.getElementById('selectedDates').value+= formatMDY(m1,x1,y1)+ 'n';
}
</script>
[/CODE]
Copy linkTweet thisAlerts:
@brenz_netFeb 26.2010 — your original code just had a typo in this line:
[CODE]
<div id="SingleCal"></div
[/CODE]


Close that div and it works in IE, too. ?
Copy linkTweet thisAlerts:
@JMRKERauthorFeb 26.2010 — Thank you Chris,

The typo was just stupidity on my part. :o

Big fingers, little keys. ?

I had not yet gotten around to checking in MSIE, but it was working in FF and Safari on my iMac.

Had to wait to get home to test on MSIE in a PC.

The function to cause the correct display in the <textarea> is a little more confusing to me.

I had not given that a thought, expecially as I had done a trial with button in this debug script that worked fine without a function call. ?
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;TArea Returns&lt;/title&gt;
&lt;script type="text/javascript"&gt;
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;textarea id="TArea"&gt;&lt;/textarea&gt;
&lt;table border="1"&gt;
&lt;tr&gt;&lt;td onclick="document.getElementById('TArea').value+='1n'"&gt;1&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td onclick="document.getElementById('TArea').value+='2n'"&gt;2&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td onclick="document.getElementById('TArea').value+='3n'"&gt;3&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td onclick="document.getElementById('TArea').value+='4n'"&gt;4&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td onclick="document.getElementById('TArea').value+='5n'"&gt;5&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;


But I'll not argue with success as your change works great.


Thank you again. ?

BTW, I removed the single quotes around the function return as I had thought the string had to be forced.
<i>
</i>function formatMDY(m,x,y) {
var MM = (m&lt;10)?'0'+m:m;
var DD = (x&lt;10)?'0'+x:x;
return MM+'/'+DD+'/'+y;
}
×

Success!

Help @JMRKER 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 4.29,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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