Hi,
I think what Paul is saying is that it is better if we give you some tips and point you in the right direction, as opposed to just giving you the solution.
So, the main problem with the code you posted is that there was no update mechanism on the input field. You could type whatever you wanted into it and your program would just sit there. One way to get around this would be to use a form:
<form id="myForm">
<input id="duration" type="text">
<input type="submit"/>
</form>
<span id="dateHolder"></span>
Now within your JavaScript we should get references to all of the things we will need:
var form = document.getElementById("myForm"),
duration = document.getElementById("duration"),
dateHolder = document.getElementById("dateHolder"),
today = new Date();
Then, we attach an onsubmit handler to the form, so that it actually does something when the user enters some input:
form.onsubmit = function(){
//Get the value from the input box - let's call it dur
var dur = ...
//Check it only contains digits
if (dur.match(/^[1-9]+$/)){
// The user entered a number.
// Do addition and set new date
}
//Prevent form's default submit action
}
I’ve left comments denoting all of the things that need doing.
You can use the regular expression to validate the user’s input and make sure they entered only digits.
After that we just need to initialize the date on page load.
dateHolder.innerHTML = today;
Here’s everything together:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Text input to update date</title>
</head>
<body>
<form id="myForm">
<input id="duration" class="duration1" type="text">
<input type="submit"/>
</form>
<span id="dateHolder"></span>
<script type="text/javascript">
// Declare variables
var form = document.getElementById("myForm"),
duration = document.getElementById("duration"),
dateHolder = document.getElementById("dateHolder"),
today = new Date();
// Attach event listener
form.onsubmit = function(){
//Get the value from the input box - let's call it dur
var dur = ...
//Check it only contains digits
if (dur.match(/^[1-9]+$/)){
// The user entered a number.
// Do addition and set new date
}
//Prevent form's default submit action
}
// Initialize date on page load
dateHolder.innerHTML = today;
</script>
</body>
</html>
Let us know how you get on 