/    Sign up×
Community /Pin to ProfileBookmark

Get the number of edited inputs

[B]Scenario[/B]

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

[CODE]<!DOCTYPE html>
<html lang=”en”>
<head>
<title>Average Grade</title>
</head>
<body>
<form>
Math: <input type=”number” id=”test1″>
<input type=”number” id=”test2″>
<input type=”number” id=”test3″>
<output id=”average”></output>
<br>
<input type=”button” value=”Calculate” id=”calcBtn”>
</form>
<script>
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;
});
</script>
</body>
</html>[/CODE]

[URL=”https://jsfiddle.net/Mori/p2m0tvfy/4/”][B]DEMO[/B][/URL]

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]

[B]Question[/B]

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

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMar 05.2019 — One possible solution.
<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&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;
&lt;style&gt;
input[type="number"] { width: 5em; }
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;form&gt;
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.
&lt;/form&gt;

&lt;script&gt;
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;
});

&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;


Note: Can be expanded upon without specific ID entries.
×

Success!

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 5.17,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...