Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 67469

How to capture number from a date input field and add digits of the sum

$
0
0

Im trying to do a few things.

1.) to understand how to write a function which will take the value placed in a date input field, and add the digits of the sum, until it is a single number.

Ex. 10/9/1940 would be 1+0+9+1+9+4+0 = 24.

2+4 = 6.

2.) Call this function when a separate button is clicked. I know how to add a click event listener to a button.

3.) Display this number with some text preceding it. ex: your number is "x"

function getSum() {
    const input = document.getElementById('dateInput').value;
    var sum = 0;
    for (var i = 0; i < input.length; i++) {
        const num = parseInt(input.charAt(i));
        if (!isNaN(num)) {
            sum += num;
        }
    }
    document.getElementById("result").textContent = "Your number is: " + sum;
}
<div class="container"><div class="cell-1"><input type="date" id="dateInput"></div><div class="cell-2"><h1>Nineborn Compatibility</h1></div><div class="cell-3"><input type="date" id="dateInput"></div><div class="cell-4" id="result"></div><div class="cell-5"><button  
onclick="getSum()">Calculate</button></div><div class="cell-6"></div><div class="cell-7"></div><div class="cell-8"></div><div class="cell-9"></div></div>

Viewing all articles
Browse latest Browse all 67469

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>