Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Display the current date in a text box after clicking a checkbox

I’m building a form to standardize filenames (I’m a video editor). After a lot of research, copying, pasting and testing I’m almost there. I just need to display the current date at the end of the filename after the user clicks on the corresponding checkbox.

The HTML has the code to get the current date and to format it as I want (YYMMDD), but for the life of me I can’t find a way to display it at the end of the filename. The code to display the date works because I can enable/disable text, but I can’t display the result of the todaysdate function.

This is the code to get the current date and format it to YYMMDD:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function SetDate()
{
var date = new Date();

var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear() - 2000;


if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;


var today = year + month + day;


document.getElementById('today').value = today;
}

This is the code that adds or removes the date at the end of the filename when you click the checkbox.

function todaysdate()
{
  var checkbox = document.getElementById('todayis');
    if (checkbox.checked != false)
        document.getElementById('todayis').value = "DATE";
    if (checkbox.checked != true)
        document.getElementById('todayis').value = "";
}

This is the code for the checkbox:

Add date (YYMMDD): <input type="checkbox" onclick="todaysdate()" id="todayis" value="" />

Thanks in advance for your help.

Edit: Added the code.

>Solution :

In your todaysdate function you set the value of the todayis input to DATE, where you should be setting it to the value of your date calculation. Here’s just a little change to what you have that should probably work!

function todaysdate()
{
  var checkbox = document.getElementById('todayis');
  if (checkbox.checked != false)
    document.getElementById('todayis').value = getDate();
  else if (checkbox.checked != true)
    document.getElementById('todayis').value = "";
}

function getDate()
{
  var date = new Date();

  var day = date.getDate();
  var month = date.getMonth() + 1;
  var year = date.getFullYear() - 2000;

  if (month < 10) month = "0" + month;
  if (day < 10) day = "0" + day;

  var today = year + "" + month + "" + day;
  return today;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading