Simple calculation with one variable - Help needed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cyberhack
    New Member
    • Mar 2007
    • 2

    #1

    Simple calculation with one variable - Help needed

    Hello!

    I m new here and also not a programmer. But i try to write a little JS for a website..
    This little script should be able to divide something. For this i wrote the following:

    <script type="text/javascript language="JavaS cript">
    function compute() {
    var divid = document.forms. divide.divident .value;
    var operator = document.forms. operator.value;
    document.forms. divide.quotient .value = divid / operator;
    }

    </script>
    <form name=divide action="javascr ipt:compute();" >
    <input type=text name=divident size=5 maxlength=5>
    <select name="operator" >
    <option value="10">10 % </option>
    <option value="15">15 % </option>
    <option value="20">20 % </option>
    </select>
    <input type=text name=quotient size=6 maxlength=6>
    <input type=submit value="Berechne n">
    <input type=reset value=Reset>
    </form>

    ... but it doesnt work. Whats wrong? Please help me to solve this.

    Best regards

    Chris
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Welcome to The Scripts.

    The problem is that you are trying to access 'operator' incorrectly. Try
    Code:
    var operator = document.forms.divide.operator.value;
    You forgot the name of the form 'divide'.

    PS: You could achieve the same functionality without a submit button, using a simple button and calling compute() in the onclick event handler.

    Comment

    • cyberhack
      New Member
      • Mar 2007
      • 2

      #3
      Originally posted by acoder
      Welcome to The Scripts.

      The problem is that you are trying to access 'operator' incorrectly. Try
      Code:
      var operator = document.forms.divide.operator.value;
      You forgot the name of the form 'divide'.

      PS: You could achieve the same functionality without a submit button, using a simple button and calling compute() in the onclick event handler.
      Hello acoder!

      Thank yo very much, i tryed your solution and it works!
      But now i realised that i have the wrong thinkings about what the script
      should do :-(

      It should to calculate a percentage of the "divident"

      An example:

      @ Divident (1st field) the user wrote 10000 and select 10 %...
      the calculation must be result in 1000...

      if the user wrote 10000 and choose 20% it results in 2000 and so on...

      Whats my error in thinking? Would you so pleased and correct my code
      that it works in the scheme above? And also i want to know what i must do that the user can only type numbers and no letters in the fields....
      But.. im not a programmer... bad bad...

      Thank you very much!

      Best regards

      Chris

      <script type="text/javascript language="JavaS cript">
      function compute() {
      var divid = document.forms. divide.divident .value;
      var operator = document.forms. divide.operator .value;
      document.forms. divide.quotient .value = divid / operator;
      }

      </script>
      <form name=divide action="javascr ipt:compute();" >
      <input type=text name=divident size=5 maxlength=5>
      <select name="operator" >
      <option value="10">10 % </option>
      <option value="15">15 % </option>
      <option value="20">20 % </option>
      </select>
      <input type=text name=quotient size=6 maxlength=6>
      <input type=submit value="Berechne n">
      <input type=reset value=Reset>
      </form>

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        That's quite simple. Just change the values in the select box to "0.somethin g", e.g. change 10 to 0.1, 20 to 0.2, etc.

        Then change the division to a multiplication:
        Code:
        document.forms.divide.quotient.value = divid * operator;

        Comment

        Working...