0

Im trying to add selected option but it doesnt seem to work.I want to be able to add selected options and display them in a text box with id = tot. At the moment it does view the value in the text box but it does not add it just views the value of the option i have selected.

<script type="text/javascript">

 function check_sm() {
        var value1 = 0;
        var sm1 = document.getElementById("slides");
        for (var i = 0; i < sm1.length; i++) {
            if (sm1[i].selected)
            { value1 = (eval(value1) + eval(sm1[i].value)); }
        } document.getElementById("SCregistration").tot.value = value1;

    }

</script>





<body>
<form>
Do you want a copy of the short course details?
<select id="slides" onChange="check_sm()">
    <option value="50">Yes</option>
    <option value="20">No</option>
</select>

</body>
</form>
3
  • What the heck are you using eval() here for? It has no place here whatsoever. value1 += parseInt(sm1.value, 10);. Commented Apr 10, 2014 at 14:40
  • Also, getElementById only returns one element, not a list of elements. Commented Apr 10, 2014 at 14:41
  • 1
    In the first line of your function , isn't var value1 = 0; supposed to be var value1 = document.getElementById("SCregistration").tot.value ? Commented Apr 10, 2014 at 14:43

1 Answer 1

1
function check_sm() {
    var slides = document.getElementById("slides");
    var selected = slides.selectedIndex;
    var optValue = parseInt(slides.options[selected].value, 10);
    var oldTot = parseInt(document.getElementById('SCregistration').tot.value, 10);
    document.getElementById('SCregistration').tot.value = oldTot + optValue;
}

DEMO

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

3 Comments

It doesnt seem like its working ... is there anything i should know?
I fixed several typos. Also, it should be slides.options, not slides.childNodes.
I checked out your demo and it works however its not working in my script i dont get whats wron... grrrr so frustrating!

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.