0

OK, new to JS, but I have a HTML form with a input box. This input box value needs to be = to a JavaScript function I wrote and I can't get it to work.

In the head I have the location of my js file. Basically, I want to have the input box value = the JavaScript function.

<script src="js/functions.js"></script>

The HTML input box

<input type="text" name="date" id= "date" value= "" class="inputBox"
 disabled />

The JavaScript function

function dateToday{
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
    dd='0'+dd
} 

if(mm<10) {
    mm='0'+mm
} 

today = mm+'/'+dd+'/'+yyyy;

document.write(today);
}

5 Answers 5

2

You can use document.getElementById("date").value to set the value of the input. Also "function dateToday{" need to be changed to "function dateToday(){"

function dateToday() {
  var today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth() + 1; //January is 0!
  var yyyy = today.getFullYear();
  if (dd < 10) {
    dd = '0' + dd
  }

  if (mm < 10) {
    mm = '0' + mm
  }

  today = mm + '/' + dd + '/' + yyyy;

  document.write(today);
  document.getElementById("date").value = today;
}

dateToday();
<input type="text" name="date" id="date" value="" class="inputBox" disabled />

Sign up to request clarification or add additional context in comments.

Comments

1
<script>
// Place this code at the end of the document body
window.onload = function(){
    document.getElementById("date").value = dateToday();    
};
</script>
</body>

And change document.write(today); to return today;

You may want to look into the date input type.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date

Comments

0

You can do document.getElementById("date").value = dateToday() as mentioned by user2182349 and also make sure to add

return today;

in your function dateToday().

2 Comments

But where would I put the document.getelement code?
You can use jquery and put do document.getelement inside document.ready function
0

Except for a few changes here and there nothing major. Check the following code.

function dateToday() {
  var today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth() + 1; //January is 0!
  var yyyy = today.getFullYear();
  if (dd < 10) {
    dd = '0' + dd
  }

  if (mm < 10) {
    mm = '0' + mm
  }

  today = mm + '/' + dd + '/' + yyyy;

  return today;
}

document.getElementById("date").value = dateToday();
<input type="text" name="date" id="date" value="" class="inputBox" disabled/>

Comments

0
<script>
    $(function() {
        document.getElementById("date").value = dateToday()
    });
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.