Help - Search - Members - Calendar
Full Version: JavaScript Help
Movable Type Community Forum > Additional Resources > Tips and Tricks
crys
Don't think what I'm trying to do is all that hard but I'm not that great with JavaScript.

I want to add a button to the edit entry screen that when clicked changes the value of the date-time input box to the current date-time. (Not to submit the new date-time, I'm often making other changes as well.)
gramcracker
I think I've got your solution, crys.  Here we go:

You'll need to open up the edit_entry file, I believe it's edit_entry.tmpl.

Put this javascript in the HEAD section:
CODE
<script language="JavaScript">

<!--

function changeDate(form)
{
var date = new Date();
var d  = date.getDate();
var day = (d < 10) ? '0' + d : d;
var m = date.getMonth() + 1;
var month = (m < 10) ? '0' + m : m;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
var totaldate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
document.thisform.created_on_manual.value = totaldate;
}
//--></script>


Then, you want to give the form a name, so insert name="thisform" into the FORM tag.

CODE
<form name="thisform" action="blah",etc>


Then, wherever you want your button or link (i just used a link), insert this code:

CODE
<a href="javascript:changeDate(thisform);">This will change the date</a>


I'm not a big javascripter, just cut and pasted code from around the Net.  It's a great way to learn to javascript!

Hope that works for you!
graham
crys
Thanks, gramcracker biggrin.gif :0

Had to edit the code a bit -- you must not use IE 6 - Win 98 'cause under that the hours were coming back as single digits which failed in MT....

CODE
function changeDate(form)
{
var date = new Date();
var d  = date.getDate();
var day = (d < 10) ? '0' + d : d;
var m = date.getMonth() + 1;
var month = (m < 10) ? '0' + m : m;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;
var h = date.getHours();
var hour = (h < 10) ? '0' + h : h;
var minute = date.getMinutes();
var second = date.getSeconds();
var totaldate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute
+ ':' + second;
document.thisform.created_on_manual.value = totaldate;
}


and for anyone who cares, heres the code to do it as a button:
CODE
<input class="button-big" type="button" value="Time->Now" onClick="changeDate(thisform);">


I though the formating came out best changing the
TMPL_IF NAME=DISP_PREFS_SHOW_AUTHORED_ON
Section to:
CODE
 <td valign="top" bgcolor="#DDDDDD"><p><font class="title">Authored on: <a href="#" onclick="openManual('item_authored_on')">(?)</a></font><br>
 <input type="hidden" name="created_on_old" value="<TMPL_VAR NAME=CREATED_ON_FORMATTED>">
 <input class="text-short" name="created_on_manual" value="<TMPL_VAR NAME=CREATED_ON_FORMATTED>"></br>
       <input class="button-big" type="button" value="Time->Now" onClick="changeDate(thisform);"></p></td>



Thanks again gramcracker...
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.