SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Updating textarea information

    This code works if I change the if/else statements to simply if statements. Why does it not work with the if/else statements? It keeps evaluating the 3rd condition as true all the time and writes in "IBM: $1250"

    Code JavaScript:
    <script language="javascript" type="text/javascript">
     
    var compCosts = new Array();
    compCosts[1] = 1000;
    compCosts[2] = 500;
    compCosts[3] = 1250;
     
     
    function up_dater()
    {
     
        var orderDetails = "";    
     
        formElement = document.form1.processor;
        if (formElement[0].checked == true)
        {
            orderDetails = "Intel : $" + compCosts[formElement[0].value];
        }    
        else if (formElement[1].checked == true)
        {
            orderDetails = "AMD : $" + compCosts[formElement[1].value];
        }
        else (formElement[2].checked == true)
        {
            orderDetails = "IBM : $" + compCosts[formElement[2].value];
        }
     
        document.form1.txtorder.value = orderDetails;    
     
     
    }
     
    </script>
    </head>
    <body>
     
    <form name=form1>
    Choose a Processor<br />
           <input type="radio" name=processor value="1" />Intel<br />
           <input type="radio" name=processor value="2" />AMD<br />
           <input type="radio" checked name=processor value="3" />IBM<br /><br />
     
            <input type="button" name="updater" value="Update Price" onclick="up_dater()" /><br />
            <textarea name="txtorder" cols="30" rows="20" id="txtorder"></textarea>
     
     
    </form>

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,226
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)

    else

    This is the first time I've seen an else with a conditonal test. Isn't the last else kind of like a default action if the preceeding if tests fail? This code doesn't throw an error?

  3. #3
    SitePoint Wizard rozner's Avatar
    Join Date
    Oct 2002
    Location
    Paris
    Posts
    1,058
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah this is definitely the problem:
    Code JavaScript:
        else (formElement[2].checked == true)
        {
            orderDetails = "IBM : $" + compCosts[formElement[2].value];
        }
    I was just testing your code and I'm surprised that it doesn't error on this, most programming languages would, but if you get rid of the condition it works fine.

    Just do:

    else {

    // rest of code

  4. #4
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just tried a few more things and double checked the error console. Not a single error. The if/else with conditionals is actually an idea out of a book I'm testing. I've checked the book errata so that probably means it should work. The third option is not really a default. One of the options will be true, its just a matter of finding which one that is.

  5. #5
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok yeah, that works. Thanks.

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,226
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)

    else

    Maybe you could change it to something like
    HTML Code:
        else if (formElement[2].checked == true)
        {
            orderDetails = "IBM : $" + compCosts[formElement[2].value];
        }
        else
        {
            orderDetails = "No Order";
        }
    Of course the "no order" would only be needed if none of the radio inputs had checked=checked. Otherwise rozner's suggestion should be fine.

  7. #7
    SitePoint Evangelist pompopom's Avatar
    Join Date
    Feb 2004
    Location
    Huldenberg (Belgium)
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what about switch() case: ?
    The Path of excess leeds to the tower of wisdom (W. Blake)

  8. #8
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I had thought about a switch statement. It makes more sense to me to use that, but I had wanted to try the if/else first. Thanks again for pointing that last else out guys.


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
  •