views:

107

answers:

1

Hi all,

I have a form with several SELECT boxes and based on the user's selection a value appears. This is fully working.

Now I want a script that calculates the SUM of these values either automatically or when pressing a button.

The full code I have so far is this:

The JavaScript:

<script type="text/javascript">
window.onload=function(a,b)
{
    value=document.getElementById('value1'),
    combobox=document.getElementById('first');

    combobox.onchange=function(a)
    {
    document.getElementById('value1').innerHTML = "&euro; " +this.value;
    }

    value2=document.getElementById('value2'), 
    combobox2=document.getElementById('second');

    combobox2.onchange=function(b)
    {
    document.getElementById('value2').innerHTML = "&euro; " +this.value;
    }
}
function changeText(){
var sum;    
sum= (value * 1) + (value2 * 1);
document.getElementById('sum').innerHTML = "&euro; " +this.value;
}
</script>

The HTML:

        <ol> 
            <li><label><span>First</span></label>
            <select name="block1" id="first"> 
                <option value="0">Please select...</option>
                <option value="1">Summer</option> 
                <option value="2">Spring</option> 
            </select></li> 
        </ol>       
        <ol> 
            <li><label><span>Second</span></label>
            <select name="block2" id="second">
                <option value="0">Please select...</option> 
                <option value="1">Summer</option> 
                <option value="2">Spring</option> 
                <option value="3">Pink</option> 
                <option value="4">Corporate</option> 
            </select></li> 
        </ol>

        <div id="value1">value 1</div>
        <div id="value2">value 2</div>

        <input type='button' onclick='changeText()' value='Calculate'/> 

        <div id="sum">Total here</div>

If anybody could help, that would be great. I really can't figure it out. Many many thanks!!

A: 

I've modified your JavaScript slightly to cater for the calculation, and now it works fine:

window.onload=function(a,b)
{
    value=document.getElementById('value1'),
    combobox=document.getElementById('first');

    combobox.onchange=function(a)
    {
    document.getElementById('value1').innerHTML = "&euro; " +this.value;
    }

    value2=document.getElementById('value2'), 
    combobox2=document.getElementById('second');

    combobox2.onchange=function(b)
    {
    document.getElementById('value2').innerHTML = "&euro; " +this.value;
    }
}
function changeText(){
var sum;    
div1Value = document.getElementById('value1').innerHTML.replace(/[^0-9]/g, '')*1;
div2Value = document.getElementById('value2').innerHTML.replace(/[^0-9]/g, '')*1;
sum = div1Value + div2Value;
document.getElementById('sum').innerHTML = "&euro; " +sum;
}​

Example here: http://jsbin.com/awici/2

However, I'd suggest you used some other approach, as this one is a bit flaky, and deal with numbers as strings.

A good approach, would be to store the values in hidden fields, and then simply calculate the values in there.

Hope this clear things out and helps you. Feel free to ask anything you don't understand

Marcos Placona