Search code examples
javascriptmysqlajaxjspnetbeans

Can we call a java function from the html text-box?


I'm doing a web project using jsp,mysql,ajax using netbeans and mysql. I'm having 3 textboxes, 2 is for taking the input from the user. The 3rd textbox should show the product of 2 input values.

How to do this? Should i make a ajax call or can i call a java function in the 3 textbox.

The code is

<input type="text" value="" name="quantity"/>
</td><td><input type="text" value="" name="price"/>
</td><td><input type="text" value="" name="total"/>

In the value attribute of the textbox named "total" can i call a java function? something like value="getTotal()",but if i can how can i access the other two values.

Otherwise should i make a ajax call?


Solution

  • no need to use java function...You can write client-side scripting

      <td><input type="text" value="" name="quantity" onblur="Calculate()"/>
      </td><td><input type="text" value="" name="price" onblur="Calculate()"/>
      </td><td><input type="text" value="" name="total"/>
    
    
      <script type="text/javascript">
    
      function Calculate()
      {
    
           var txt1 = document.getElementById("quantity");
           var txt2 = document.getElementById("price");
           var txt3 = document.getElementById("total");
           if ((txt1.value != "") && (txt2.value != ""))
           {
                txt3.value = parseInt(txt1.value) * parseInt(txt2.value); 
           }
    
      }
    
      </script>
    

    Let's, keep the total textbox as a read-only or use label...

    thanks