Jump to content

[SOLVED] JAVAscript Problem :(


hungryOrb

Recommended Posts

Hello!

 

I DO realise this is a PHP forum, but this is just posted here in the vain hope that one or two may know enough to right the code below!

 

I would like to create a date button which when clicked inserts todays date in a specified format into the text box with id="datebox" to it's left.

 

Code:

 

			<b>Date(yyyy-mm-dd):</b></td><td class=two>
			<input type="text" name="dateofsubmit" size="8" id="datebox">


			<script type="text/javascript">
			<!--
			var currentTime = new Date()
			var month = currentTime.getMonth() + 1
			var day = currentTime.getDate()
			var year = currentTime.getFullYear()
			document.write(year + "-" + month + "-" + day)
			//-->
			</script>

<button onclick="document.getElementById('datebox').value=
document.write(year + "-" + month + "-" + day)">Today</button>

 

 

It doesn't work of course! However, one thing to note, is that because all this lies inside a <form></form> the button automatically adopts the characteristics of the other button on the page which is a submit button. Anyone know a way to make this button only have the insert date ability?

 

Thankyou in advance!

Robin

Link to comment
https://forums.phpfreaks.com/topic/72756-solved-javascript-problem/
Share on other sites

this is a php forum yeah but youre in the javascript section so you came to the right place. Something i always use when working with forms i just place a hyperlink inside the form so it wont submit. to make it look like a button just place an image inside. This is the easiest way.

You can also write a script that stops the form from submitting when a button is clicked but I recommend using a hyperlink

<form>
<a href="javascript:jsFunction()">place time</a>
</form>

Thanks, fixed it though ;]

here is result for whoever can use it also:

 

<script type="text/javascript">
			<!--
			var currentTime = new Date()
			var month = currentTime.getMonth() + 1
			var day = currentTime.getDate()
			var year = currentTime.getFullYear()
							//-->
			</script>

<input type=button onclick="document.getElementById('datebox').value=year + '-' + month + '-' + day" value="Today">

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.