SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru hifigrafix's Avatar
    Join Date
    Oct 2005
    Location
    Nashville, TN
    Posts
    628
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Toggle w/ SELECT onChange

    Greetings.

    I've got a shopping cart for customers in Canada and US. I've got a select box that triggers an onChange event to add a $5.95 shipping charge if someone chooses "Canada" from my list.

    It's all working great except say someone wants to change back to United States then the onChange event is still adding the $5.95 shipping charge.

    Can someone point me in the right direction to toggle between adding $5.95 and removing for each change made.

    HTML Code:
    <script type="text/javascript">
    window.onload=document.checkout.submit();
    
    function change() { 
    document.forms[0].ship.value = "$5.95";
    var total = document.forms[0].total.value;
    var newtotal = <?php echo $bigtotal; ?> + 5.95;
    document.forms[0].total.value = "$" + newtotal.toFixed(2);
    }
    </script>
    I don't know JavaScript but wrote this from scratch so please excuse my mess and backwards approach.

    Thanks
    jw

  2. #2
    SitePoint Member therealfiredog69's Avatar
    Join Date
    Aug 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the check box is calling the change() function each time it is changed, then you need to add an IF statement in the change() funciton. The IF statement should check to see if the check box is checked or not. If so, then add the shipping.. If not then just do the remaining calculations...


    Code:
    function change() {
    
    if document.forms[0].checked
         document.forms[0].ship.value = "$5.95";
    
    var total = document.forms[0].total.value;
    var newtotal = <?php echo $bigtotal; ?> + 5.95;
    document.forms[0].total.value = "$" + newtotal.toFixed(2);
    }


Bookmarks

Posting Permissions

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