0

I have attempted to implement a function call when a dropdown menu is changed. I think my problem is I am thinking too much in the programming style of C++. I have researched the question and have not found any similar answers.

My code is as follows:

JS:

<script type="text/javascript">         

        var type = 0;
        var milk = 0;
        var extra = 0;

        settype(t) {
            type = t;
        }

        setmilk(m) {
            milk = m;
        }

        setextra(e) {
            extra = e;
        }

        function calculateCalories() {
            return (type*milk)+extra;
        }
        function reset() {
            type = 0;
            milk = 0;
            extra = 0;
        }
</script>

HTML:

<table border=4 width=800px>
<tr>
<td align=right>
<h2 style="text-align: left"> Your Coffee Drink: </h2>
<input type="text" name="Input" size="35" style="text-align: right"/>
Calories                       
</td>
</tr>

<td>
Calculate the calories of your favorite coffee drink! *
</td>

<td>
Select Type:
    <select id="coffeetypedropdown" onChange="settype(value);">
        <option value="">Select a Drink</option>
        <option value="0">Caffe</option>
        <option value="1">Caffe Latte</option>
        <option value="1">Caffe Macchiato</option>
        <option value="0">Caffe Americano</option>
        <option value=".5">Cappuccino</option>
    </select>
</td>

<td>
Select Type of Milk
    <select id="milkdropdown" onchange="setmilk(value);">
        <option value="">Select Type of Milk</option>
        <option value="80">Nonfat</option>
        <option value="100">1% Milk</option>
        <option value="120">2% Milk</option>
        <option value="150">Whole Milk</option>
        <option value="130">Soy Milk</option>
    </select>

</td>

<td>
Select Extra
    <select id="extradropdown" onchange="setextra(value);">
        <option value="">Select Extra</option>
        <option value="100">Whipped Cream</option>
        <option value="100">Chocolate Syrup</option>
        <option value="50">Caramel Syrup</option>
        <option value="10">Chocolate Powder</option>
        <option value="100">Heavy Cream</option>
    </select>

</td>


<td>
<input type="button" name="clear" VALUE="Clear" OnClick="Calc.Input.value = reset();">
<input type="button" name="Equals"  VALUE="Calculate!" OnClick="Calc.Input.value =    eval(calcCalories();)">
</td>



</td>

Thank you very much for the help.

3
  • 1
    try 'this.value' instead of just 'value' e.g. onchange="setextra(this.value);" and onchange="settype(this.value);" Commented Apr 16, 2014 at 10:19
  • 1
    What is Calc? And calcCalories u used in onclick method does not match your actual function's name calculateCalories Commented Apr 16, 2014 at 10:21
  • Thank you. It was a combination of misspelling (I apologize), the this.value mentioned by @Arvind, and placing function before the setters. Commented Apr 16, 2014 at 10:26

1 Answer 1

3

Please add function before function name.

<script>
    function settype(t) {
       alert(t);
    }
</script>

Let me know if any concern. (You want to add 'settype' functional globally? - same as global variable)

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

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.