#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2002
    Posts
    548
    Rep Power
    0

    Why isn't this simple JavaScript code working?


    Racking my brain here ....

    Code:
    <script>function viewcom() {
    
    if (document.commission.models.selectedIndex.value eq '6000P') {
    document.commission.amount.value = '$1.00'
    }
    
    if (document.commission.models.selectedIndex.value eq '6000') {
    document.commission.amount.value = '$0.90'
    }
    
    }</script>
    <form name=commission><select name=models onChange="viewcom()"><option value="">- Select a model -</option>
    <option value='6000P'>6000P</option>
    <option value='6000'>6000</option>
    
    &nbsp;&nbsp;<input name=amount></form>
    Last edited by RandyL712; October 18th, 2002 at 05:48 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    document.commission.models.selectedIndex returns an integer (the selected index of the models select element. You then try to access the value member of the returned integer, which doesn't exist.

    You need to get the index, then use the models array with that index number to get the value.
    Code:
    document.commission.models[document.commission.models.selectedIndex].value
    Also, you never had a closing select tag. Furthermore, eq is a Perl operator, and doesn't exist in javascript (which I'm assuming you're trying to use, since you never specified a language in your script element). It's also common practice to put scripts in your head element.

    Try this:
    Code:
    <html>
    <head>
    <script language="Javascript">
    function viewcom()
    {
    if (document.commission.models[document.commission.models.selectedIndex].value == '6000P') {
    document.commission.amount.value = '$1.00'
    }
    
    if (document.commission.models[document.commission.models.selectedIndex].value == '6000') {
    document.commission.amount.value = '$0.90'
    }
    
    }
    </script>
    </head>
    <body>
    <form name=commission><select name=models onChange="viewcom()"><option value="">- Select a model -</option>
    <option value='6000P'>6000P</option>
    <option value='6000'>6000</option>
    </select>
    <input name=amount></form>
    </body>
    </html>
  4. #3
  5. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    <script type="text/javascript" language="javascript">

    function viewcom(sel) {
    var v = sel[sel.selectedIndex].value;
    switch (v) {
    case '6000P' :
    sel.form.amount.value = '$1.00';
    break;
    case '6000':
    sel.form.amount.value = '$0.90';
    break;
    }
    }

    </script>
    ............
    <form name="commission">
    <select name="models" onchange="viewcom(this)">
    <option value="">- Select a model -</option>
    <option value="6000P">6000P</option>
    <option value="6000">6000</option>
    </select>
    <input name="amount" type="text">
    </form>
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2002
    Posts
    548
    Rep Power
    0
    Thanks - I'm having trouble going back and forth from JavaScript to Perl...
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    It's funny because I had the same problem when I went from Perl to PHP.

IMN logo majestic logo threadwatch logo seochat tools logo