Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Javascript button to insert/add to a form field.



8:39 am on Mar 12, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member woz is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 13, 2000
votes: 0

I have a page with a form that contains information to be entered into a database. One field is a date field which automatically retrieves the stored date, or, if no date stored enters today's date, when the form is loaded.

I would like to have a couple of buttons, which I am assuming will need javascript, to allow the user to
Button 1) replace the field contents with the date x days later than the current field contents, or
Button 2) replace the field contents with the date x days from today.

For the purpose of the exercise, lets assume x = 7 days.

Wondering if anyone can give me any pointers on how to do this as I don't know much Javascript. Also, what would be the difference in coding between replacing the field contents or adding to the current field contents in, say, a text field.

(sounds a bit like an exam question 'ey?)


3:10 pm on Mar 12, 2002 (gmt 0)

New User

10+ Year Member

joined:Mar 11, 2002
votes: 0

I hope this is what u r looking for

Post the following script in the <head> tag.

<script type="text/javascript">
function getAssignDate( when )
var x = 7;
mydate = new Date();
mymonth = mydate.getMonth();
myweekday = mydate.getDate();
myyear = mydate.getYear();
if (myyear < 1000) myyear += 1900;
months = new Array ("January","February","March","April","May","June","July","August","September","October","November","December");//define arrays
var toThisField = document.myForm.thisDate;
if( 'newDate' == when )
toThisField.value = months[mymonth] + " " + new Number( myweekday + x ) + ", " + myyear;

toThisField.value = months[mymonth] + " " + myweekday + ", " + myyear;

And this in the <html>
<body onload="getAssignDate( 'today' )">

<form name="myForm">
<input type="text" name="thisDate" value="" />
<input type="button" value="Click to enter new date" onclick="getAssignDate( 'newDate' );" />


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members