Log in

View Full Version : putting javascript in a disabled text box in php..



Humper
09-07-2006, 02:40 PM
I cant seem to figure out how to put this javascript code into the disabled text box... the javascript code automatically fills in todays date..

here is the disabled text box

<tr><td width="73">Event Date:</td>
<td width="715"><input name=date type=text disabled value="Todays Date">
</td></tr>


and here is the javascript code

<SCRIPT Language="JavaScript">
<!-- hide from old browsers
var today = new Date()
var month = today.getMonth()+1
var year = today.getYear()
var day = today.getDate()
if(day<10) day = "0" + day
if(month<10) month= "0" + month
if(year<1000) year+=1900

document.write(month + "/" + day +
"/" + year)
//-->
</SCRIPT>

can someone show me how to put in it the disabled text box

sandman
09-07-2006, 06:07 PM
You are writing to the page, not to the form element.
So instead of document.write, use document.forms

var today = new Date()
var month = today.getMonth()+1
var year = today.getYear()
var day = today.getDate()
if(day<10) day = "0" + day
if(month<10) month= "0" + month
if(year<1000) year+=1900

yourDate = month+'/'+year
document.forms[0].date.value = yourDate;

Twey
09-08-2006, 09:20 PM
<script type="text/javascript">
Number.prototype.pad = function(n) {
var l = this.toString();
while(l.length < n)
l = "0" + l;
return l;
};

window.onload = function() {
var c;
document.forms[0].elements['date'].value = ((c = new Date()).getMonth() + 1).pad(2) + "/" + c.getFullYear().pad(2);
};
</script>Just output that anywhere on the page. Note that it assumes your form is the first one on the page; if it isn't, change the number or give your form a name and reference it by that.