0

So I can not make the input into a integer. I want to turn this into a int so then it can calculate the age of you. I have been researching but can not find the right answer. HTML CODE:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>learn</title>
<script src="script.js"></script>
</head>
<form action="/learn.html">
    <b>Year Born:</b> <input id="input988744" type="value">
</form>
<button onClick="calculateAge(birthYear);">Summit</button>
<body>
</body>
</html>

Javascript Code:

/*
Function
*/

// var
var birthYear;
birthYear = document.getElementById("input988744");

// function
function calculateAge(birthYearInFun) {
    "use strict";
    console.log(2018 - birthYearInFun); // make it a number
}
1
  • On what line? @Mirodinho Commented Oct 28, 2018 at 1:10

3 Answers 3

4

2 things needs to be changed

  1. HTML:Change type of <input> from text to number, number can be negative so can add parameter min="1900" to stop from entering date earlier then 1900

    <b>Year Born:</b> <input id="input988744" type="number" min="1900">
    
  2. Javascript: Need to fetch value of HTML element, thus add .value adter getElementById, as getElementById gives HTML element not its value. To be on safer side use parseInt() or Number() over birthYear

    var birthYear;
    birthYear = Number(document.getElementById("input988744").value);
    or
    birthYear = parseInt(document.getElementById("input988744").value);
    

Both these will make birthYear number.

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

Comments

1

What you really want it looks like is the input type to be a date... Check out the snippet below

legend {
    background-color: #000;
    color: #fff;
    padding: 3px 6px;
}

.output {
    font: 1rem 'Fira Sans', sans-serif;
}

input {
    margin: .4rem;
}

label {
    display: inline-block;
    text-align: right;
    width: 20%;
}
<fieldset>
    <legend>Choose trip dates</legend>

    <div>
        <label for="start">Start</label>
        <input type="date" id="start" name="trip"
               value="2018-07-22"
               min="2018-01-01" max="2018-12-31" />
    </div>

    <div>
        <label for="end">End</label>
        <input type="date" id="end" name="trip"
               value="2018-07-29"
               min="2018-01-01" max="2018-12-31"/ >
    </div>

</fieldset>

Comments

1

You can access the value of the input field as birthYearInFun.value as Mirodinho said and then if it a string, convert it to a number using parseInt().

birthYear = parseInt(document.getElementById("input988744").value)

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.