最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make a input in HTML in JS to integer - Stack Overflow

programmeradmin0浏览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
}

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
}
Share Improve this question asked Oct 28, 2018 at 1:02 George DavisGeorge Davis 191 gold badge2 silver badges6 bronze badges 1
  • On what line? @Mirodinho – George Davis Commented Oct 28, 2018 at 1:10
Add a ment  | 

3 Answers 3

Reset to default 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.

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>

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)

发布评论

评论列表(0)

  1. 暂无评论