Press Button to display (the value in the button) on the Input Box.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • divina11
    New Member
    • Aug 2007
    • 55

    Press Button to display (the value in the button) on the Input Box.

    I'm building a program where you have an input box and an button, when user presses the button, the button value is displayed on the input box. I've tried the following:

    [code=JavaScript]

    function calculation() {
    value1=parseInt (document.getEl ementById("x"). value); //input box
    nine=parseInt(d ocument.getElem entById("nine") .value); // button

    value1=nine;
    }

    [/code]

    What I'm I doing wrong?
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Divina.

    The problem you're having is that when you use parseInt(), you're no longer working with a reference to x's value; instead you're using a copy of it.

    To make this work, you'll want to do something like this:
    [code=javascript]
    function calculation() {
    value1=document .getElementById ("x").value; //input box
    nine=parseInt(d ocument.getElem entById("nine") .value); // button

    value1=nine;
    }
    [/code]

    Note that since you never actually do anything with value1's value, there is no need to parseInt() it anyway.

    You're looking for [code=javascript], by the way ~_^

    Comment

    • divina11
      New Member
      • Aug 2007
      • 55

      #3
      Acoder, I've tried the what you said and still it didn't work, Can you see my coding below, (thank you for your quick response):


      [CODE=javascript]
      <html>
      <head>
      <script>
      function calculation() {
      value1=document .getElementById ("x").value;
      nine1=parseInt( document.getEle mentById("nine" ).value);

      value1=nine1;
      }
      </script>

      </head>
      <body>

      <input type="text" id="x" size="25"/>
      <input type="button" id="nine" value="9" onclick="calcul ation()">

      </body>
      </html>[/CODE]

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Divina.

        Hm. Ok, try this:
        [code=javascript]
        function calculation() {
        value1=document .getElementById ("x"); //input box
        nine=parseInt(d ocument.getElem entById("nine") .value); // button

        value1.value = nine;
        }
        [/code]

        Comment

        • divina11
          New Member
          • Aug 2007
          • 55

          #5
          Thank you Acoder that worked, thanks alot again.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by divina11
            Thank you Acoder that worked, thanks alot again.
            Glad you got it working, but I should point out it was pbmods that helped you!

            Comment

            • divina11
              New Member
              • Aug 2007
              • 55

              #7
              Final post of the day, In my Calculator Program I press the button 9, then I press button +, then I press 8, then I press button =, which will then display the result (17) in the text box.

              Without using Objects, what is the best way to get this working. Please see code below:


              [CODE=javascript]<html>
              <head>
              <script>

              function digit8() {
              value1=document .getElementById ("x");
              Eight=parseInt( document.getEle mentById("eight ").value);

              value1.value=Ei ght;
              }

              function digit9() {
              value1=document .getElementById ("x");
              Nine=parseInt(d ocument.getElem entById("nine") .value);

              value1.value=Ni ne;
              }

              function equals() {
              value1=document .getElementById ("x");
              Nine=parseInt(d ocument.getElem entById("nine") .value);
              Eight=parseInt( document.getEle mentById("eight ").value);
              }

              </script>

              </head>
              <body><br>

              <input type="text" id="x" size="25"/>
              <input type="button" id="eight" value=" 8 " onclick="digit8 ()">
              <input type="button" id="add" value=" + " onclick="">
              <input type="button" id="nine" value=" 9 " onclick="digit9 ()"><br><br>

              <input type="button" id="equals" value=" = "onclick="" >

              </body>
              </html>[/CODE]

              Comment

              • pbmods
                Recognized Expert Expert
                • Apr 2007
                • 5821

                #8
                Originally posted by acoder
                Glad you got it working, but I should point out it was pbmods that helped you!
                lol It's the eyes.

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  Heya, Divina.

                  You'll need to build up an array of operands and values, then join() and eval() them.

                  First, you need to create a set of numerical buttons:
                  [code=html]
                  <input type="button" onclick="addToC alc(this.value) ;" value="1" />
                  etc.
                  [/code]

                  Then you need to set up your environment:
                  [code=javascript]
                  var calc = [];

                  function addToCalc(value )
                  {
                  /***************
                  *
                  * Pop the last value off of calc if possible.
                  * It makes it easier to process that way.
                  */
                  var last = '';
                  if(calc.length > 0)
                  {
                  last = calc.pop();
                  }

                  switch(value)
                  {
                  /***************
                  *
                  * Operands terminate values.
                  */
                  case '-':
                  case '*':
                  case '/':
                  case '+':
                  switch(last)
                  {
                  /***************
                  *
                  * Change operand if User pressed 'c' or hasn't entered a number yet.
                  *
                  * E.g., 1+ --> 1-
                  */
                  case '-':
                  case '*':
                  case '/':
                  case '+':
                  calc.push(value );
                  break;

                  /***************
                  *
                  * Otherwise add the operand.
                  *
                  * E.g., 1+1 --> 1+1-
                  */
                  default:
                  calc.push(last) ;
                  calc.push(value );
                  break;
                  }
                  break;

                  /***************
                  *
                  * The clear button removes the last item.
                  *
                  * E.g., 1+12345 --> 1+
                  * (or) 1+ --> 1
                  */
                  case 'c':
                  break;

                  /***************
                  *
                  * Numbers get appended to the current item.
                  *
                  * E.g., 1+1 --> 1+12
                  * (or) 1+ --> 1+1
                  */
                  default:
                  switch(last)
                  {
                  /***************
                  *
                  * If the last 'value' was an operand, put it back on and add the next value.
                  */
                  case '-':
                  case '*':
                  case '/':
                  case '+':
                  calc.push(last) ;
                  calc.push(value );
                  break;

                  /***************
                  *
                  * Otherwise append the value to what's currently there.
                  */
                  default:
                  calc.push(Strin g(last) + String(value));
                  break;
                  }
                  break;
                  }

                  // For debugging
                  alert(calc.join (' '));
                  }
                  [/code]

                  Then you can create a calculate button:
                  [code=html]
                  <input type="button" onclick="calcul ate();" value="=" />
                  [/code]

                  [code=javascript]
                  function calculate()
                  {
                  var result = eval(calc.join( ''));
                  delete calc;
                  calc = [];

                  // For debugging
                  alert(result);
                  }
                  [/code]

                  As this sounds suspiciously like a homework problem, I strongly advise you to ask me lots of questions so that you understand why this works. You (and your instructor!) will be a lot happier that way.

                  Comment

                  • divina11
                    New Member
                    • Aug 2007
                    • 55

                    #10
                    I don't understant the comment: "Change operand if User pressed 'c' or hasn't entered a number yet".

                    Ta

                    Comment

                    • pbmods
                      Recognized Expert Expert
                      • Apr 2007
                      • 5821

                      #11
                      Heya, Divina.

                      Originally posted by divina11
                      I don't understant the comment: "Change operand if User pressed 'c' or hasn't entered a number yet".
                      Think about how a physical calculator works. Suppose you were to press '1', then '+'. Then suppose you changed your mind and pressed '-'.

                      The way the calculator would handle that would be to replace the '+' with a '-' in the expression.

                      Similarly, the goal here is to replace the current operand with the new one.

                      For example, suppose your calc array looked like this:
                      [code=javascript]
                      calc = [
                      '12',
                      '+',
                      '15',
                      '*'
                      ];
                      [/code]

                      If the User presses the '-' button, you want to change calc to this:
                      [code=javascript]
                      calc = [
                      '12',
                      '+',
                      '15',
                      '-' <--
                      ];
                      [/code]

                      When addToCalc('-') runs, it pops off the last element in calc.
                      [code=javascript]
                      calc = [
                      '12',
                      '+',
                      '15'
                      ];

                      last = '*';
                      [/code]

                      Then, we enter the switch statement, and we hit the case '-' (line 22).

                      So we switch(last) (line 26). Since it is an operand (case '*', line 35), we ignore the value of last and instead push the '-' onto calc (line 38):
                      [code=javascript]
                      calc = [
                      '12',
                      '+',
                      '15',
                      '-'
                      ];

                      last = '*';
                      [/code]

                      Then we break, go to the alert statement, and you should see this:
                      12 + 15 -

                      Comment

                      • divina11
                        New Member
                        • Aug 2007
                        • 55

                        #12
                        Sorry Sorry Sorry Pbmods for your hard work, I think it's a bit complex for me, I think there is an easier way but I've not sure how to implement it.

                        I was thinking user presses 8 + 9 =

                        Hence for each of these buttons I store it in a variable and then when user presses =, I do the sum when user presses =

                        Can you help me implement this?

                        Ta

                        Comment

                        • pbmods
                          Recognized Expert Expert
                          • Apr 2007
                          • 5821

                          #13
                          Heya, Divina.

                          No problem. I had fun writing it anyway.

                          Sounds like what you'll want to do is create four textboxes:
                          [code=html]
                          <input id="number1" value="" />
                          <input id="operand" value="" />
                          <input id="number2" value="" />
                          <input id="answer" value="" />
                          [/code]

                          I presume you want the User to use number buttons instead of being able to type in the numbers; is this correct?

                          Similarly to what you were doing before, you'll want to store a reference to the text box that you want to edit:
                          [code=javascript]
                          /***************
                          *
                          * Initialize textbox as a global variable; we start with the first number.
                          */
                          var current = document.getEle mentById('numbe r1');
                          [/code]

                          [code=html]
                          <input type="button" id="eight" value=" 8 " onclick="digit( 8);">
                          <input type="button" id="add" value=" + " onclick="digit( '+');">
                          <input type="button" id="nine" value=" 9 " onclick="digit( 9);"><br><br>
                          [/code]

                          To implement the digit() function:
                          [code=javascript]
                          function digit(value)
                          {
                          /***************
                          *
                          * Check to see if value is a number. If it is, append it to current.value.
                          *
                          * If it is an operand, set the value of the 'operand' textbox and then change current to reference the 'number2' textbox.
                          */
                          }
                          [/code]

                          Comment

                          • divina11
                            New Member
                            • Aug 2007
                            • 55

                            #14
                            y is a Global Variable to store operators (+,/,*,-), however i can't get the 'if' statement to work below:

                            [CODE=javascript]function equals() {
                            value1=document .getElementById ("v");

                            if (y=="+") { // I think the problem is here!!!!
                            alert("+ was selected");
                            var answer = (x+z);
                            value1.value=an swer;
                            }
                            else if (y=="*") { // I think the problem is here too!!!!
                            alert("* was selected");
                            }
                            }[/CODE]

                            Comment

                            • pbmods
                              Recognized Expert Expert
                              • Apr 2007
                              • 5821

                              #15
                              Heya, Divina.

                              [CODE=javascript] works better than [CODE : javascript] ~_^

                              Try alert()ing y to see if it has a value.

                              Comment

                              Working...