/    Sign up×
Community /Pin to ProfileBookmark

Get the number of edited inputs


Every semester my students need to take at least one test. The following form gives the right average grade of a student:

<html lang=”en”>
<title>Average Grade</title>
Math: <input type=”number” id=”test1″>
<input type=”number” id=”test2″>
<input type=”number” id=”test3″>
<output id=”average”></output>
<input type=”button” value=”Calculate” id=”calcBtn”>
document.getElementById(‘calcBtn’).addEventListener(‘click’, function() {
var test1 = document.getElementById(‘test1’).value;
var test2 = document.getElementById(‘test2’).value;
var test3 = document.getElementById(‘test3’).value;
var average = document.getElementById(‘average’);
average.value = (Number(test1)+Number(test2)+Number(test3)) / 3;


The problem is it works right only if all the fields are edited. If the student doesn’t take some tests, the average grade won’t show the right value. I know it’s because of dividing by the fixed number [I]3[/I] when it calculates the average grade:

[CODE]average.value = (Number(test1)+Number(test2)+Number(test3)) / 3;[/CODE]


What is a simple approach to get the number of [I]changed[/I] input fields?

to post a comment

1 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMar 05.2019 — One possible solution.
</i>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;title&gt;Average Grade&lt;/title&gt;
&lt;!-- From: https://www.webdeveloper.com/forum/d/384071-get-the-number-of-edited-inputs --&gt;
input[type="number"] { width: 5em; }

Math: &lt;input type="number" min="0" max="100"&gt;
&lt;input type="number" min="0" max="100"&gt;
&lt;input type="number" min="0" max="100"&gt;
&lt;p&gt; &lt;input type="button" value="Calculate" id="calcBtn"&gt; Average:
&lt;input id="average" value='' readonly size="6"&gt; for
&lt;input id="cnt" value='' size="3" readonly&gt; entries.

document.getElementById('calcBtn').addEventListener('click', function() {
var sel = document.querySelectorAll('input[type="number"]'),
sum = 0,
cnt = 0,
v = 0;
for (let el of sel) { v = Number(el.value); if (!isNaN(v) &amp;&amp; (v != 0)) { sum += v; cnt++; } }
document.getElementById('average').value = (cnt != 0) ? (sum/cnt).toFixed(2) : '0.00';
document.getElementById('cnt').value = cnt;


Note: Can be expanded upon without specific ID entries.


Help @Rain_Lover spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 11.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ

legal: ({
terms: of use,
privacy: policy
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
recent_tips: (
tipper: @bahaedd,
tipped: article
amount: 1000 SATS,

tipper: @Balmasexy,
tipped: article
amount: 1000 SATS,

tipper: @mbsaad,
tipped: article
amount: 1000 SATS,