Argha Guha Roy
Argha Guha Roy

Reputation: 9

can not add two multiplications result in text box in html

sir i have 7 text boxes .1st two take values from user and display the multiplication result on 3rd text box when it clicked.4th and 5th textboxes also take values and display the multiplication result in 6th text box when it clicked.now 7th textbox will display the addition result when it clicked.7th textbox takes the values from 3rd and 6th textbox. my problem is that i can not do the addition and cant display the result in 7th text box in html and jscript.plz help me.....i am attached the code...

 <html>
  <script>
   function getText1(){
      var in1=document.getElementById('in1').value;
      var in2=document.getElementById('in2').value;


      var tot1=parseInt(in1)*parseInt(in2); 


      document.getElementById('tot1').value=tot1;
   }


   function getText2(){


      var in1=document.getElementById('in3').value;
      var in2=document.getElementById('in4').value; 

      var tot2=parseInt(in1)*parseInt(in2);           

      document.getElementById('tot2').value=tot2;
   }

   function gt1(){

   var in1=document.getElementById('tot1').value; 
   var in2=document.getElementById('tot2').value;

   var gt1=parseInt(in1)+parseInt(in2);
   document.getElementById('gt').value = gt1;



  </script>

 <form>
 <input type="text"id="in1"/>
 <input type="text" id="in2"/>
 <input type="text" onclick="getText1()" id="tot1"/>
 <br>
 <input type="text"id="in3"/>
 <input type="text" id="in4"/>
 <input type="text" onclick="getText2()" id="tot2"/>
  <br>
 <input type="text" onclick="gt1()" id="gt1"/>

</form>
</html>

Upvotes: 0

Views: 114

Answers (5)

Regent
Regent

Reputation: 5178

There are several problem

1). Element id is one line is incorrect. It should be:

document.getElementById('gt1').value = gt1;

2). result variable is undefined. It should be:

if (!isNaN(gt1))

3). onclick="gt1()" will not work (because there is id with the same name gt1). Use, for example:

function getText3() { ... }

and

<input type="text" onclick="getText3()" id="gt1"/>

Fiddle.

Update. Ok, I didn't notice that you want multiplication, but somewhy had written + in getText1() and getText2() functions. Then you should also replace these two + with *:

Multiplication fiddle.

Upvotes: 1

Yatin Mistry
Yatin Mistry

Reputation: 100

Check the below code.

<script >
function getext3(){
    txt1 = document.getElementById("text1").value;
    txt2 = document.getElementById("text2").value;
    document.getElementById("text3").value = parseInt(txt1)*parseInt(txt2);

} 

function getext6(){
    txt1 = document.getElementById("text4").value;
    txt2 = document.getElementById("text5").value;
    document.getElementById("text6").value = parseInt(txt1)*parseInt(txt2);

} 
function getext7(){


    txt1 = document.getElementById("text3").value;
    txt2 = document.getElementById("text6").value;
    document.getElementById("text7").value = parseInt(txt1)+parseInt(txt2);

} 
</script>

Text1 : <input type="text" id="text1" value="5"> <br/>
Text2 : <input type="text" id="text2" value="2"> <br/>
Text3 : <input type="text" id="text3" value="" onclick="getext3()"> <br/>


Text4 : <input type="text" id="text4" value="7"> <br/>
Text5 : <input type="text" id="text5" value="10"> <br/>
Text6 : <input type="text" id="text6" value="" onclick="getext6()"> <br/>

Text7 : <input type="text" id="text7" value="" onclick="getext7()"> <br/>

Upvotes: 0

Maneesh Singh
Maneesh Singh

Reputation: 575

<html>
  <script>
   function getText1(){
      var in1=document.getElementById('in1').value;
      var in2=document.getElementById('in2').value;


      var tot1=parseInt(in1)*parseInt(in2); 


      document.getElementById('tot1').value=tot1;
   }


   function getText2(){


      var in1=document.getElementById('in3').value;
      var in2=document.getElementById('in4').value; 

      var tot2=parseInt(in1)*parseInt(in2);           

      document.getElementById('tot2').value=tot2;
   }

   function gt(){

   var in1=document.getElementById('tot1').value; 
   var in2=document.getElementById('tot2').value;

   var gt1=parseInt(in1)+parseInt(in2);
   document.getElementById('gt1').value = gt1;
}


  </script>

 <form>
 <input type="text"id="in1"/>
 <input type="text" id="in2"/>
 <input type="text" onclick="getText1()" id="tot1"/>
 <br>
 <input type="text"id="in3"/>
 <input type="text" id="in4"/>
 <input type="text" onclick="getText2()" id="tot2"/>
  <br>
 <input type="text" onclick="gt()" id="gt1"/>

</form>
</html>

use this code it is working. and compare with own code. thanks..

Upvotes: 1

Aria
Aria

Reputation: 3844

You have not defined 'result' variable and in 'gt1()' :

document.getElementById('gt').value = gt1;

1- There is no 'gt' element in the page

2- 'gt1' should renamed to 'result' and at the end code will be:

<script language="javascript" type="text/javascript">
    function getText1() {
        var in1 = document.getElementById('in1').value;
        var in2 = document.getElementById('in2').value;
        var tot1 = parseInt(in1) + parseInt(in2);
        document.getElementById('tot1').value = tot1;
    }
    function getText2() {
        var in1 = document.getElementById('in3').value;
        var in2 = document.getElementById('in4').value;
        var tot2 = parseInt(in1) + parseInt(in2);
        document.getElementById('tot2').value = tot2;
    }

    function gt1() {
        var in1 = document.getElementById('tot1').value;
        var in2 = document.getElementById('tot2').value;
        var result = parseInt(in1) + parseInt(in2);
        if (!isNaN(result)) {
            document.getElementById('gt1').value = result;
        }
        //document.getElementById('gt1').value=gt1;
    } 
</script>

this is work properly.

Upvotes: 0

aleha_84
aleha_84

Reputation: 8539

You dont have element with id 'gt'.

Change last line of code to this:

document.getElementById('gt1').value = gt1;

Upvotes: 0

Related Questions