Results 1 to 2 of 2

Thread: Get the number of edited inputs

  1. #1
    Join Date
    Nov 2009
    Location
    Isfahan, Iran
    Posts
    229
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default Get the number of edited inputs

    Scenario

    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>
    DEMO

    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 3 when it calculates the average grade:
    Code:
    average.value = (Number(test1)+Number(test2)+Number(test3)) / 3;
    Question

    What is a simple approach to get the number of changed input fields?

  2. #2
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,414
    Thanks
    1
    Thanked 245 Times in 240 Posts

    Default


    One possible solution...

    ~ the original bald headed old fart ~

Similar Threads

  1. Resolved Edited DD forums
    By keyboard in forum Picture Gallery
    Replies: 2
    Last Post: 05-22-2012, 03:20 AM
  2. indexof help EDITED
    By chopper in forum JavaScript
    Replies: 3
    Last Post: 04-25-2008, 11:36 PM
  3. A Ghost edited my html
    By ikon in forum HTML
    Replies: 12
    Last Post: 11-02-2007, 05:46 PM
  4. [DHTML] Edited Form field Progress Bar
    By Master_script_maker in forum Submit a DHTML or CSS code
    Replies: 2
    Last Post: 06-13-2007, 10:33 PM
  5. Replies: 6
    Last Post: 11-26-2006, 05:58 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •