JavaScript CheckBox Issues

ssc456

Fully Optimized
Messages
4,280
Hey guys,

I'm rather a novice when it comes to JavaScript so please let me know if there is a better way of doing this but.

I've got two check boxes that when clicked I want to check which one was checked and then change another value on a page, nice and simple.

However I've noticed that my checkboxes if clicked in the right place can trigger the javascript function without changed the ischecked value?

Is there something wrong here or is it a known problem that you need to cover off by manually changing the ischecked in the javascript?

Code:
<script type='text/javascript'>
            function testFunction(mystr) {
                if (document.getElementById(mystr).checked >= 1) 
                {
                    if(mystr == "chk1")
                    {
                        document.getElementById("mytot").firstChild.nodeValue = parseFloat(document.getElementById("mytot").firstChild.nodeValue) + parseFloat("10.00")
                    }
                    else
                    {
                        document.getElementById("mytot").firstChild.nodeValue = parseFloat(document.getElementById("mytot").firstChild.nodeValue) + parseFloat("50.00")
                    }
                }
                else {
                    if (mystr == "chk1") {
                        document.getElementById("mytot").firstChild.nodeValue = parseFloat(document.getElementById("mytot").firstChild.nodeValue) - parseFloat("10.00")
                    }
                    else {
                        document.getElementById("mytot").firstChild.nodeValue = parseFloat(document.getElementById("mytot").firstChild.nodeValue) - parseFloat("50.00")
                    }
                }
            }
        </script>

    <input type="checkbox" onclick="testFunction('chk1')"; id="chk1" /> Check Box 1 - £10
    <br />
    <input type="checkbox" onclick="testFunction('chk2')"; id="chk2" /> Check Box 2 - £50
    <p id="mytot">0.00</p>
 

ssc456

Fully Optimized
Messages
4,280
Never mind guys, solved it silly error!

Changed the functions from onClick to onChange.

Problem solved, it appears there are areas on the checkbox which will trigger the click but not tick the box!
 
Top Bottom