Go Back   Computer Forums > Web Design | Website Development > Web Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 06-18-2013, 08:50 AM   #1
Fully Optimized
 
ssc456's Avatar
 
Join Date: Jan 2007
Posts: 4,279
Send a message via MSN to ssc456
Default JavaScript CheckBox Issues

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>
__________________

__________________
He who has never failed has never attempted anything worth succeeding at.

Dont Eat Animals, Its Not Good For Them And They Dont Like It!
ssc456 is offline   Reply With Quote
Old 06-19-2013, 10:00 AM   #2
Fully Optimized
 
ssc456's Avatar
 
Join Date: Jan 2007
Posts: 4,279
Send a message via MSN to ssc456
Default Re: JavaScript CheckBox Issues

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!
__________________
He who has never failed has never attempted anything worth succeeding at.

Dont Eat Animals, Its Not Good For Them And They Dont Like It!
ssc456 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 08:21 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0