0

Okay, so I have three text boxes. My goal is to have a fourth text box that is generated when the user clicks the button. The fourth text box should be populated with the input from the other three boxes. I've managed to do this with the code here:

<html>
   <head></head>
   <body>
      <script>
         function addTextBox() {
         var element = document.createElement("input");

         element.setAttribute("type", "text");
         element.setAttribute("id", "Text4");
         document.body.appendChild(element);

         fill();
         }

         function fill() {
         input = document.getElementById("Text1").value + " " + document.getElementById("Text2").value + " is " + document.getElementById("Text3").value;;;
         document.getElementById('Text4').value = input;
         }

      </script>
      <form>
         <input type="text" id="Text1" value="Firstname">
         <input type="text" id="Text2" value="Lastname">
         <input type="text" id="Text3" value="Age">
         <input type="button" onclick="addTextBox()">
      </form>
   </body>
</html>

But there's one last thing I need to do. When the user inputs their age in the third textbox, I need to have in converted to their age in days before it appears in the fourth box.

Can anybody help me do this?

Mithun
  • 7,747
  • 6
  • 52
  • 68
Lou
  • 47
  • 2
  • 5
  • How could you manage to convert an age which is in years to days? It would be very very gross: you could be 355 days wrong... you cannot obtain the days only from the years. – Marco Bonelli Apr 01 '15 at 06:13
  • 2
    use date of birth instead of age in years to get age in days – Vivek Gupta Apr 01 '15 at 06:15
  • This is for an assignment and it says that the user enters their age and their age in days should be returned. Is there really no way to do this? Perhaps I misunderstood. – Lou Apr 01 '15 at 06:18
  • possible duplicate of [How do I get the number of days between two dates in JavaScript?](http://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – dvhh Apr 01 '15 at 06:24
  • No. No dates are involved. Just the number of years and the number of days. – Lou Apr 01 '15 at 06:25

1 Answers1

2

Hey you can use below function to calcuate no of days by passing age

 function totalDays(age)
    {
    var now = new Date();
    var start = new Date();
    start.setFullYear((now.getFullYear()-age))
    var diff = now - start;
    var oneDay = 1000 * 60 * 60 * 24;
    var day = Math.floor(diff / oneDay);
    return day ;
    }
Murli
  • 1,258
  • 9
  • 20