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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Location
    under my desk
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Select value always returns 0

    I have four select boxes and I want to add the values of the last three. For some reason, the values are always 0.
    I've tested this in Firefox 3 and IE 7 and neither give me any errors, so I guess it's just grabbing the value before it's changed.
    I would really appreciate any advice on how to fix this. Thanks in advance!


    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
        <head>
            <title></title>
    
            <style type="text/css">
                form.c1 {display:none}
                form.c2 {display:inline}
            </style>
            <script type="text/javascript">
                window.onload  = function() {
                    document.seasonform.season.selectedIndex = '0';
                    document.lengthoform.lengtho.selectedIndex = '0';
                    document.lengthpform.lengthp.selectedIndex = '0';
                    document.petform.pet.selectedIndex = '0';
                }
                function showL() {
                    if (document.seasonform.season.selectedIndex == '0') {
                        document.lengthoform.style.display = "none";
                        document.lengthpform.style.display = "none";
                        document.lengthoform.lengtho.selectedIndex = '0';
                        document.lengthpform.lengthp.selectedIndex = '0';
                        document.petform.style.display = "none";
                        document.petform.pet.selectedIndex = '0';
                    }
                    if (document.seasonform.season.selectedIndex == '1') {
                        document.lengthoform.lengtho.selectedIndex = '0';
                        document.lengthoform.style.display = "none";
                        document.lengthpform.style.display = "inline";
                    }
                    if (document.seasonform.season.selectedIndex == '2') {
                        document.lengthpform.lengthp.selectedIndex = '0';
                        document.lengthpform.style.display = "none";
                        document.lengthoform.style.display = "inline";
                    }
                }
                function showPp() {
                    if (document.lengthpform.lengthp.selectedIndex == '0') {
                        document.petform.style.display = "none";
                        document.petform.pet.selectedIndex = '0';
                    }
                    if (document.lengthpform.lengthp.selectedIndex != '0') {
                        document.petform.style.display = "inline";
                    }
                }
                function showPo() {
                    if (document.lengthoform.lengtho.selectedIndex == '0') {
                        document.petform.style.display = "none";
                        document.petform.pet.selectedIndex = '0';
                    }
                    if (document.lengthoform.lengtho.selectedIndex != '0') {
                        document.petform.style.display = "inline";
                    }
                }
                function showPaypal() {
                    if (document.petform.pet.selectedindex = '0') {
                        document.paypal.style.display = "none";
                    }
                    if (document.petform.pet.selectedindex = '1') {
    					document.paypal.style.display = "inline";
    				}
    				if (document.petform.pet.selectedindex = '2') {
    					document.paypal.style.display = "inline";
    				}
                }
                function total() {
                    this.lp = document.lengthpform.lengthp.value;
                    this.lo = document.lengthoform.lengtho.value;
                    this.pet = document.petform.pet.value;
                }
            </script>
        </head>
        <body>
            <script type="text/javascript">
            </script>
            <form name="seasonform" action="" class="c2">
                <select name="season" onChange="showL();">
                    <option value="0">Select One</option>
                    <option value="peak">Peak Season</option>
                    <option value="off">Off Season</option>
                </select>
            </form>
            <br>
            <form name="lengthpform" action="" class="c1">
                <select name="lengthp" onChange="showPp();">
                    <option value="0">Select One</option>
                    <option value="595">2 Days</option>
                    <option value="760">3 Days</option>
                    <option value="925">4 Days</option>
                    <option value="1080">5 Days</option>
                    <option value="1245">6 Days</option>
                    <option value="1400">1 Week</option>
                </select>
            </form>
            <form name="lengthoform" action="" class="c1">
                <select name="lengtho" onChange="showPo();">
                    <option value="0">Select One</option>
                    <option value="696">2 Days</option>
                    <option value="720">3 Days</option>
                    <option value="845">4 Days</option>
                    <option value="970">5 Days</option>
                    <option value="1095">6 Days</option>
                    <option value="1200">1 Week</option>
                </select>
            </form>
            <br>        
            <form name="petform" action="" class="c1">
                <select name="pet" onChange="showPaypal();">
                    <option value="0">Select One</option>
                    <option value="0">No</option>
                    <option value="100">Yes</option>
                </select>
            </form>
            <form name="paypal" action="" class="c1">
            <script type="text/javascript">
    		var V = new total();
    		document.write(V.lp);
    		document.write(V.lo);
    		document.write(V.pet);
                    // This is written so that I can see the variables. It does not need to be here when the problem is fixed.
            </script>
            </form>
        </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Apr 2008
    Location
    New Jersey
    Posts
    116
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Your code is mostly fine. Your difficulty is just that you're thinking that the JS in this:
    Code:
            <form name="paypal" action="" class="c1">
            <script type="text/javascript">
    		var V = new total();
    		document.write(V.lp);
    		document.write(V.lo);
    		document.write(V.pet);
                    // This is written so that I can see the variables. It does not need to be here when the problem is fixed.
            </script>
            </form>
    is being executed when you display that block. It's not. It's executed when the page first loads, when all the values are 0. So when it displays, you're thinking it's telling you your values are 0, but it's not, it's telling you your values were 0 when the page first loaded. If you change that debugging output to a function and run it when you set the paypal block to display: inline, you'll be fine.
    Chaos
    Lost Souls: text based RPG | MUDseek: MUD gaming search | MUDfind: MUD resource sites | Discordian Quotes

  • Users who have thanked chaosprime for this post:

    mrwireful9000 (08-23-2008)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    The function total() is never called except on window.onload()

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,298
    Thanks
    10
    Thanked 584 Times in 565 Posts
    the bug is here:

    Code:
                function showPaypal() {
                    if (document.petform.pet.selectedindex = '0') {
                        document.paypal.style.display = "none";
                    }
                    if (document.petform.pet.selectedindex = '1') {
    					document.paypal.style.display = "inline";
    				}
    				if (document.petform.pet.selectedindex = '2') {
    					document.paypal.style.display = "inline";
    				}
                }
    when you call:
    Code:
     if (document.petform.pet.selectedindex = '0')
    you are actually assigning the selected index a value of 0, as you observed.

    remember to use the comparison "==" operator, not the assignment "=" operator:
    Code:
     if ( document.petform.pet.selectedIndex == '0')
    in fact, you really could simply use :
    Code:
     if (!document.petform.pet.selectedIndex )
    hope this help you, i know how frustrating one-char bugs are!!!



    EDIT: it's also "selectedIndex" not "selectedindex", you need to fix this to.
    Last edited by rnd me; 08-21-2008 at 10:14 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    New to the CF scene
    Join Date
    Aug 2008
    Location
    under my desk
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks everyone! I re-wrote it with a button, because I finally gave up on making it work without one. The only problem now is that when I add the values together, they add as a string, not as numbers. is there any way to fix this? I tried adding a " + 0" to the end, but that didn't help.

    Here is my new code:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
        <head>
            <title></title>
    
            <style type="text/css">
                form.c1 {display:none}
                form.c2 {display:inline}
            </style>
            <script type="text/javascript">
                window.onload  = function() {
                    document.seasonform.season.selectedIndex = '0';
                    document.lengthoform.lengtho.selectedIndex = '0';
                    document.lengthpform.lengthp.selectedIndex = '0';
                    document.petform.pet.selectedIndex = '0';
                }
                function total() {
                    amt = document.lengthpform.lengthp.value + document.lengthoform.lengtho.value + document.petform.pet.value;
                }
                function showL() {
                    if (document.seasonform.season.selectedIndex == '0') {
                        document.lengthoform.style.display = "none";
                        document.lengthpform.style.display = "none";
                        document.lengthoform.lengtho.selectedIndex = '0';
                        document.lengthpform.lengthp.selectedIndex = '0';
                        document.petform.style.display = "none";
                        document.petform.pet.selectedIndex = '0';
                    }
                    if (document.seasonform.season.selectedIndex == '1') {
                        document.lengthoform.lengtho.selectedIndex = '0';
                        document.lengthoform.style.display = "none";
                        document.lengthpform.style.display = "inline";
                    }
                    if (document.seasonform.season.selectedIndex == '2') {
                        document.lengthpform.lengthp.selectedIndex = '0';
                        document.lengthpform.style.display = "none";
                        document.lengthoform.style.display = "inline";
                    }
                }
                function showPp() {
                    if (document.lengthpform.lengthp.selectedIndex == '0') {
                        document.petform.style.display = "none";
                        document.petform.pet.selectedIndex = '0';
                    }
                    if (document.lengthpform.lengthp.selectedIndex != '0') {
                        document.petform.style.display = "inline";
                    }
                }
                function showPo() {
                    if (document.lengthoform.lengtho.selectedIndex == '0') {
                        document.petform.style.display = "none";
                        document.petform.pet.selectedIndex = '0';
                    }
                    if (document.lengthoform.lengtho.selectedIndex != '0') {
                        document.petform.style.display = "inline";
                    }
                }
                function showPaypal() {
                    if (document.petform.pet.selectedindex = '0') {
                        document.paypal.style.display = "none";
                    }
                    if (document.petform.pet.selectedindex = '1') {
    					document.paypal.style.display = "inline";
    					total();
    				}
    				if (document.petform.pet.selectedindex = '2') {
    					document.paypal.style.display = "inline";
    					total();
    				}
                }
                function button() {
                    document.write(amt);
                }
            </script>
        </head>
        <body>
            <script type="text/javascript">
            </script>
            <form name="seasonform" action="" class="c2">
                <select name="season" onChange="showL();">
                    <option value="0">Select One</option>
                    <option value="peak">Peak Season</option>
                    <option value="off">Off Season</option>
                </select>
            </form>
            <br>
            <form name="lengthpform" action="" class="c1">
                <select name="lengthp" onChange="showPp();">
                    <option value="0">Select One</option>
                    <option value="595">2 Days</option>
                    <option value="760">3 Days</option>
                    <option value="925">4 Days</option>
                    <option value="1080">5 Days</option>
                    <option value="1245">6 Days</option>
                    <option value="1400">1 Week</option>
                </select>
            </form>
            <form name="lengthoform" action="" class="c1">
                <select name="lengtho" onChange="showPo();">
                    <option value="0">Select One</option>
                    <option value="696">2 Days</option>
                    <option value="720">3 Days</option>
                    <option value="845">4 Days</option>
                    <option value="970">5 Days</option>
                    <option value="1095">6 Days</option>
                    <option value="1200">1 Week</option>
                </select>
            </form>
            <br>        
            <form name="petform" action="" class="c1">
                <select name="pet" onChange="showPaypal();">
                    <option value="0">Select One</option>
                    <option value="0">No</option>
                    <option value="100">Yes</option>
                </select>
            </form>
            <br>
            <form name="paypal" action="" class="c1">
            <input type="button" name="gen" value="Click here!" onClick="button();"></input>
            <script type="text/javascript">
            
            </script>
            </form>
        </body>
    </html>

  • #6
    Regular Coder
    Join Date
    Apr 2008
    Location
    New Jersey
    Posts
    116
    Thanks
    0
    Thanked 29 Times in 29 Posts
    You need to do parseInt(val1) + parseInt(val2) etc.
    Chaos
    Lost Souls: text based RPG | MUDseek: MUD gaming search | MUDfind: MUD resource sites | Discordian Quotes

  • #7
    New Coder
    Join Date
    Jul 2008
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post
    You could also use " x -= -y", since subtracting a negative is the same as adding.

  • #8
    New to the CF scene
    Join Date
    Aug 2008
    Location
    under my desk
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks guys!


  •  

    Posting Permissions

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