Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2002
    Location
    Oakland, CA
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    onclick handler in checkbox

    Hi,

    Still learning js, so bear with me!

    I have a form that enables a user to add optional features to a plan using checkboxes. When they check or uncheck the checkbox for the add-on, the Total Monthly Cost amount is updated in a span below. That part of it seems to be working fine. I'm having trouble with the onclick event however, in that if checked, it won't uncheck when clicked upon, and if I click one the other one gets checked also.

    Any help will be greatly appreciated!

    <script>

    ---other script that deals with display: none or inline for two of the checkbox/label combos below (FEATURED_CL and OFF_CL)---

    function totalcost(addonselected) {
    var tc = Number(document.getElementById('prices').getElementsByTagName('span')[1].firstChild.nodeValue);
    addonselected.checked = !(addonselected.checked);
    if (document.getElementById('ENOTE_CL').checked = true) {
    var addon1 = Number(document.getElementById('prices').getElementsByTagName('span')[2].firstChild.nodeValue);
    }
    if (document.getElementById('FEATURED_CL').checked = true) {
    var addon2 = Number(document.getElementById('prices').getElementsByTagName('span')[3].firstChild.nodeValue);
    }
    else if (document.getElementById('OFF_CL').checked = true) {
    var addon2 = Number(document.getElementById('prices').getElementsByTagName('span')[3].firstChild.nodeValue);
    }
    tc = tc + addon1 + addon2;
    document.getElementById('items').getElementsByTagName('span')[0].firstChild.nodeValue = tc;
    }
    </script>

    <body>
    <form>

    ---other stuff---

    <div id="items">
    <input type="checkbox" id="setup" name="products" value="setup" checked="checked" disabled="disabled" class="check" /><label for="setup">SETUP FEE<br /></label>
    <input type="checkbox" id="search" name="products" value="search" checked="checked" disabled="disabled" class="check" /><label for="search">SEARCH<br /></label>
    <input type="checkbox" id="ENOTE_CL" name="products" value="ENOTE_CL" class="check" onclick="totalcost(this);" /><label for="ENOTE_CL">EMAIL NOTIFIER<br /></label>
    <input type="checkbox" id="FEATURED_CL" name="products" value="FEATURED_CL" class="check" onclick="return totalcost(this);" /><label for="FEATURED_CL">FEATURED ADS<br /></label>
    <input type="checkbox" id="OFF_CL" name="products" value="OFF_CL" style="display:none;" class="check" onclick="return totalcost(this);" /><label for="OFF_CL" style="display:none;">OFFICE SOLUTION<br /></label>
    <br />
    Total Monthly Cost: $<span>22.95</span><br />
    </div>
    <div id="prices">
    $<span>59.00</span> OT<br />
    <span>22.95</span>/mo<br />
    +<span>10.00</span>/mo<br />
    +<span>10.00</span>/mo<br />
    </div>

    ---other stuff---

    </form>
    </body>

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Why do you have this?
    Code:
    addonselected.checked = !(addonselected.checked);
    That will make the checkbox "unclickable". And also the equality symbol in the condition is == and not =.
    Code:
    if (document.getElementById('ENOTE_CL').checked == true) {
      var addon1 = Number(document.getElementById('prices').getElementsByTagName('span')[2].firstChild.nodeValue);
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New Coder
    Join Date
    Oct 2002
    Location
    Oakland, CA
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for the reply.

    I implemented your changes, along with a couple other small fixes, and it's now functional. I appreciate it!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •