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 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2004
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Passing form <option> values to javascript

    Hi.

    I'm trying to create a form for users to submit a value in a text box and choose a unit from a drop down menu. On submitting, the script will then carry out certain conversions of the inputted value based on the unit that was chosen from the drop down menu and output the converted values. This is what i've got so far:

    <html>
    <head>
    <script type="text/javascript">
    function cal(form) {if (document.form1.unit1.selectedIndex = 0){
    var i = document.form1.text1.value/3
    var a = document.form1.text1.value/2
    document.form1.answer.value = i
    document.form1.answer1.value = a}
    else if (document.form1.unit1.selectedIndex = 1)
    {var i = document.form1.text1.value/2
    var a = document.form1.text1.value/3
    document.form1.answer.value = i
    document.form1.answer1.value = a}
    }
    </script>
    </head>

    <body>
    Input a value and choose calculate:
    <form name="form1">
    <input type="text" name="text1">&nbsp;&nbsp;
    <select name="unit1">
    <option selected>Joules</option>
    <option>eV</option>
    <option>Hartree</option>
    </select>
    <input name="button" type="button" onClick="cal(this.form)" value="Calculate">
    <br>
    <br>
    Answers:<br>
    <input type="text" name="answer">
    &nbsp;Joules<br>
    <input type="text" name="answer1">
    &nbsp;eV
    </form>
    </body>
    </html>

    The problem is that i can't seem to get the script to respond to the option chosen from the drop down menu. It responds it all kinds of weird (wrong) ways. If think the problem is with the bold parts of the code. Can anyone help?

    Thanks.

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Where you have

    document.form1.unit1.selectedIndex = 0

    and

    document.form1.unit1.selectedIndex = 1

    it should be

    document.form1.unit1.selectedIndex == 0

    and

    document.form1.unit1.selectedIndex == 1

    Note the 2 equal signs

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    And you're already passing the form reference to the function.
    So you don't need to do

    document.form1

    but just

    form

    which is the variable you used for the function parameter.

    And you can take out these 2 lines outside of the if-else condition as those will be executed whatever the selection is.

    document.form1.answer.value = i
    document.form1.answer1.value = a

    Just put them after the if-else condition.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    New Coder
    Join Date
    Jun 2004
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks. The script now works fine.


  •  

    Posting Permissions

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