Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with a function

Status
Not open for further replies.

milams

Programmer
May 28, 2004
32
US
Hi,

I wrote this function and the function works as long as I pass along the input fields that it needs to run the function. I've been looking at function examples and I see a lot of using "this" in the function argument. I try to do this and it never works. It always says that "Object Expected". Can someone explain to me why using this doesn't work? I know there is an easier way to pass values into functions other that listing all of the input names in the argument. Is there?

Code:
    <script language="javascript" type="text/javascript">
    
        function calculateShip(item_quantity_1, ship_method_price_1){
                    
            if(document.buynow.item_quantity_1.value <= 25){
                var grdship = 7.95;
            }
            else if((document.buynow.item_quantity_1.value >= 26) && (document.buynow.item_quantity_1.value <= 50)){
                var grdship = 10.95;
            }
            else if((document.buynow.item_quantity_1.value >= 51) && (document.buynow.item_quantity_1.value <= 75)){
                var grdship = 13.95;
            }
            else if((document.buynow.item_quantity_1.value >= 76) && (document.buynow.item_quantity_1.value <= 100)){
                var grdship = 16.95;
            }
            else if(document.buynow.item_quantity_1.value >= 100){
                var grdship = 20.95;
            }
            
            document.buynow.ship_method_price_1.value = grdship;
            
        }//end of function
    </script>

HTML
Code:
<input type="text" name="item_quantity_1" value="0" size="3" onchange="return calculateShip(item_quantity_1.value, ship_method_price_1.value)"/>

Thanks
 
change your call to something like:

Code:
<input type="text" name="item_quantity_1" value="0" size="3" onchange="return calculateShip(this, this.form.ship_method_price_1)"/>

and your function to something like:

Code:
function calculateShip(q, s){
            
    if(q.value <= 25){
        var grdship = 7.95;
    }
    else if((q.value >= 26) && (q.value <= 50)){
        var grdship = 10.95;
    }
    else if((q.value >= 51) && (q.value <= 75)){
        var grdship = 13.95;
    }
    else if((q.value >= 76) && (q.value <= 100)){
        var grdship = 16.95;
    }
    else if(q.value >= 100){
        var grdship = 20.95;
    }
    
    s.value = grdship;
    
}

consequently, i'd also use a switch statement in place of your if/elseif/else statement. that's just me though.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Thanks cLFlaVA,

That worked. Do you know of any kind resource that gives good explainations of functions? Because the books that I've read don't really go into depth about Functions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top