You must Sign In to post a response.
  • Category: JQuery

    Substraction between two value with jquery keypress event.

    Hi ! i want to substract textbox2 value from textbox1 and display result in textbox3 in asp.net with jquery.

    i write the following code on textbox2 keypress

    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function () {
    $("#TextBox2").keydown(function () {
    var a = $("#TextBox1").val();

    var b = $("#TextBox2").val();

    var c = a - b;

    $("#TextBox3").val(c);
    });
    });
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:Panel ID="Panel1" class="Panel" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    </asp:Panel>
    </div>
    </form>

    this working but not properly. its result such as

    40 - 10 = 39(e.i when i press 3 it does not fire,when press zero then substract 40 - 1 not 40 - 10)
    400 - 100 = 390
    400 - 10 = 300
    please give me a solution.thanks all.
  • #758903
    Hi Rashed,

    Refer the below code:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
    <script type="text/javascript">
    $(function (){
    $("#<%=TextBox2.ClientID %>").focusout(function () {
    var diff = $("#TextBox1").val() - $("#TextBox2").val();
    $("#TextBox3").val(diff);
    });
    });
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:Panel ID="Panel1" class="Panel" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br/>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br/>
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    </asp:Panel>
    </div>
    </form>
    </body>
    </html>

    I set it on focusout event on keydown event it will not work because value of textbox2 has not been set.

    Hope this will help you.
    Mark the answer if it helped you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "If you can dream it, you can do it."

  • #758907

    Hi,

    Jquery keypress event

    Usually you can write code for keyup like this

    $(document).keyup(function(e){
    console.log(e.which);
    });

    Keydown : keeps firing when user holds the keys down, while keypress and keyup fire only once.

    keypress : doesn't detect special keys (e.g. SHIFT), keydown and keyup do

    In theory, the keydown and keyup events represent keys being pressed or released, while the keypress event represents a character being typed. The implementation of the theory is not same in all browsers

    Check the following links for keyup and keydown

    http://howtodoinjava.com/2013/12/20/jquery-difference-between-keypress-and-keydown-events/
    http://www.aspdotnet-suresh.com/2013/09/jquery-keypress-keydown-keyup-events.html

  • #758917
    There is bit problem in keypress event, keypress event will fire each time when you press key so, when you want to subtract 10 from 40, Then you press 1 first and then 0, Apparently it will subtract 1 from 40 which result in 39 and then 0 from 39 so the result will be 39 only.
    you need to use OnBlur event on textbox2 so that it will fire when you lift focus from textbox

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • Sign In to post your comments