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 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Detroit MI
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Correct usage of 'else'?

    I'm trying to write a simple script (I hope its simple):[list=1][*]If the value of the select box is 1001, then I want to assign the variable TEST1 a value of 50[*]If the value is not 1001 then I would like TEST1 to equal 0.[*]The same goes for TEST2[/list=1]
    The problem I am having TEST1 is ALWAYS 50 and TEST2 is ALWAYS 70, so I have a sneaking suspicion that I am misusing else, or just leaving something out.
    Code:
    function ComputeTotal(form)
    	{
    
    if (document.form.test.value = 1001) 
    	TEST1 = 50
    else
    	TEST1 = 0
    
    if (document.form.test.value = 1002)
    	TEST2 = 70
    else
    	TEST2 = 0
    
    	subtotal = (TEST2 + TEST1)
    	
    	document.form.total.value = subtotal
    	}
    Eventually there will be more than 2 options per select box and I need the value to be an inventory number instead of just the mere price.

    Also while I'm here, I have another question: Would it be possible to have a non numerical value for the options in select box "test" and if so, how would I define them? (I tried making the values "testa" and "testb" but it kept coming back saying that "testa is undefined")

    Forgive me for my "noobness".

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Delaware, USA
    Posts
    137
    Thanks
    0
    Thanked 0 Times in 0 Posts
    else if fine it's your test.

    you were assigning the value versus testing it. Change from single = to a double =

    ie
    if (document.form.test.value == 1001)

  • #3
    New Coder
    Join Date
    Jul 2002
    Location
    Detroit MI
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I knew it was something simple, that's what I get for refusing to take any type of computer class.

  • #4
    New Coder
    Join Date
    Jul 2002
    Location
    Detroit MI
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Being the doof that I am, I realized after typing about 15 if (blah blah blah)s, that an array would work perfectly. So for future reference this is an example of how I accomplished it:
    Code:
    var TESTvalue = new Array()
    TESTvalue[0] = 50
    TESTvalue[1] = 70
    
    var TEST1value = new Array()
    TEST1value[0] = 500
    TEST1value[1] = 700
    
    function ComputeTotal()
    {
    TEST = TESTvalue[form.test.selectedIndex]
    TEST1 = TEST1value[form.test1.selectedIndex]
    
    total = (TEST + TEST1)
    
    form.total.value = total
    }
    </HEAD>
    <BODY onLoad="ComputeTotal()">
    
    <form name="form">
    <select name="test" size="1" onChange="ComputeTotal()">
    <option value="whatever">$50</option>
    <option value="whatever">$70</option>
    </select>
    <select name="test1" size="1" onChange="ComputeTotal()">
    <option value="whatever">$500</option>
    <option value="whatever">$700</option>
    </select>
    <input type="text" name="total">
    </form>
    oopsie, typo
    Last edited by pinkotoad; 07-27-2002 at 01:31 AM.

  • #5
    New Coder
    Join Date
    Jul 2002
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you find your script not working try adding " at the end of ComputeTotal() at lines

    <select name="test" size="1" onChange="ComputeTotal()>

    and

    <select name="test1" size="1" onChange="ComputeTotal()>

    like so

    <select name="test" size="1" onChange="ComputeTotal()">
    <select name="test1" size="1" onChange="ComputeTotal()">


  •  

    Posting Permissions

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