0

I am creating a form that has select inputs and the values add up to a certain score. I can't get the values from the select to add and the sum into the total input to show the score of their test.

jscript:

function calculate_total(){
        var elems = document.getElementsByClassName('Assessment');
        var myLength = elems.length,
        total = 0;

        for (var i = 0; i < myLength; ++i) {
          total += parseInt(elems[i].value * 1);
        }
        document.getElementById('ScoreTot').value = total;

}

html:

<cfselect name="SELECT1" id="select1" class="Assessment">
    <option value="0"> OPTION 1 </option>
    <option value="1"> OPTION 2 </option>
</cfselect>

<cfselect name="select2" id="select2" class="Assessment">
    <option value="0"> OPTION 1 </option>
    <option value="1"> OPTION 2 </option>
    <option value="2"> OPTION 3 </option>
</cfselect>

<cfselect name="select3" id="select3" class="Assessment">
    <option value="0"> OPTION 1 </option>
    <option value="1"> OPTION 2 </option>
    <option value="2"> OPTION 3 </option>
</cfselect>

Score: <cfinput type="text" name="ScoreTot" id="ScoreTot" value="">
3
  • What is cfselect ? Commented Apr 30, 2015 at 18:24
  • You also never call your calculate_total function Commented Apr 30, 2015 at 18:25
  • In addition to everything else, you should change your for loop increment from ++i to i++ Commented Apr 30, 2015 at 18:29

4 Answers 4

1

There are some errors in your logic. Below is a working copy, but first a few notes:

  1. You've got ColdFusion HTML here or something, HTML does not by itself know was a <cfselect> or <cfinput> is, so this example shows valid HTML.
  2. You're not calling your function anywhere. Make sure you do!

function calculate_total() {
    var els = document.getElementsByClassName('Assessment'); 
    // always use var to declare variables in scope; they are global otherwise
    var total1 = 0;

    // Little benefit to storing the length elsewhere
    // Use i++ instead of ++i
    for (var i = 0; i < els.length; i++) {
        // Use a radix to indicate base 10, I didn't see a need for the *1
        total1 += parseInt(els[i].value, 10);
    }
    document.getElementById('ScoreTot').value = total1;

}

// Call your function somehow!
calculate_total();
<select name="SELECT1" id="select1" class="Assessment">
    <option value="0">OPTION 1</option>
    <option value="1" selected>OPTION 2</option>
</select>
<select name="select2" id="select2" class="Assessment">
    <option value="0">OPTION 1</option>
    <option value="1">OPTION 2</option>
    <option value="2" selected>OPTION 3</option>
</select>
<select name="select3" id="select3" class="Assessment">
    <option value="0">OPTION 1</option>
    <option value="1">OPTION 2</option>
    <option value="2" selected>OPTION 3</option>
</select>Score:
<input type="text" name="ScoreTot" id="ScoreTot" value="">

Sign up to request clarification or add additional context in comments.

Comments

1

With selects, you need to use the value of the option that is selected especially if you plan to serve older browsers.

elems[i].options[elems[i].selectedIndex].value

And, as another answer points out, parseInt should have a radix.

 function calculate_total(){
    var elems = document.getElementsByClassName('Assessment');
    var myLength = elems.length,
    total = 0;

    for (var i = 0; i < myLength; ++i) {
      total += parseInt(elems[i].options[elems[i].selectedIndex].value * 1,10);
    }
    document.getElementById('ScoreTot').value = total;
}

3 Comments

This solution worked. I had no idea select values were handled differently, thanks.
In modern browsers, the value on a select list itself will reflect the selected option's value.
Agreed, it's on paper that .value works in ie7+, but I've found it to be unreliable even in modern browsers. a few more keystrokes ensures it works. The MDN states that it will work with .value, but in the example given, also uses the selectedIndex method: developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement
0

try

 parseInt( number, 10) 

parseInt needs a radix

Comments

0

Here it is not clear when you want the function to be executed. Add a button to the dom and attach an event to it.

var e = document.getElementById('foo');
e.onclick = calculateTotal;

function calculateTotal(){
    
        var elems = document.getElementsByClassName('Assessment');
        var myLength = elems.length,
        total = 0;
        for (var i = 0; i < myLength; ++i) {
          total += parseInt(elems[i].value * 1);
        }
        document.getElementById('ScoreTot').value = total;

}
<select name="SELECT1" id="select1" class="Assessment">
    <option value="0"> OPTION 1 </option>
    <option value="1"> OPTION 2 </option>
</select>

<select name="select2" id="select2" class="Assessment">
    <option value="0"> OPTION 1 </option>
    <option value="1"> OPTION 2 </option>
    <option value="2"> OPTION 3 </option>
</select>

<select name="select3" id="select3" class="Assessment">
    <option value="0"> OPTION 1 </option>
    <option value="1"> OPTION 2 </option>
    <option value="2"> OPTION 3 </option>
</select>

Score: <input type="text" name="ScoreTot" id="ScoreTot" value="">

<input type="button" id="foo" value="Add" />

Here is the working example https://jsfiddle.net/u3sj6euy/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.