rick williams
rick williams

Reputation: 1

multiply textbox value by label and dynamically populate the result in another textbox

I have two textboxes and a hidden label.
Normally I do this with asp.net:

After a user enters the number in the first textboxt1, then after clicking the button getprice the system multiply the textbox1 value by the hidden label number and show the result in the textbox2 and vice versa.

But I want to achieve this same thing without using a button postback by using either jquery or angularjs.
As the user is typing the figures on the textbox1, the system should be multiplying that value at runtime and showing it in textbox2 on keypress.

I need a function to this please.

I have tried using this jQuery... Its not working.
I want the function to have a name, so I can put on keypress on textbox1.

<script type="text/javascript">
  $(function () {
    $("#<%=Textbox.ClientID%>").bind("input", function() {
      var value1 = $("#<%=Textbox.ClientID%>").val()
      var value2 = $("#<%=HiddenTextbox2.ClientID%>").val();
      $("#<%=Textbox3.ClientID%>").val(value1 * value2);
    });
  });
</script>

Upvotes: 0

Views: 1006

Answers (3)

$("#pay,#pre").keyup(function () {

$('#pay').val($('#pay').val() * $('#pre').val());

});

Blockquote

Upvotes: 0

Wenadin
Wenadin

Reputation: 396

I guess it would be easier just to answer using JavaScript. Here's a function that would work:

<script type="text/javascript">
    function MultiplyByHidden() {
        var value1 = parseFloat(document.getElementById("<%=Textbox.ClientId%>").value)
        var value2 = parseFloat(document.getElementById("<%=HiddenTextbox2.ClientId%>").value)
        var total = value1 * value2 
        document.getElementById("<%=Textbox3.ClientId%>").value = total
    }
</script>

This function does everything you are looking for and doesn't require importing of extra libraries like JQuery to handle such simple functionality.

Edit I forgot that it would multiply strings too.

Upvotes: 1

Louys Patrice Bessette
Louys Patrice Bessette

Reputation: 33933

I think you try to multiply strings...

The use of parseInt() or parseFloat() is recommanded for numbers in text inputs ;)

<script type="text/javascript">
  $(function () {
    $("#<%=Textbox.ClientID%>").on("input", function() {
      var value1 = parseFloat( $("#<%=Textbox.ClientID%>").val() );
      var value2 = parseFloat( $("#<%=HiddenTextbox2.ClientID%>").val() );
      $("#<%=Textbox3.ClientID%>").val(value1 * value2);
    });
  });
</script>

Upvotes: 0

Related Questions