locked
Jquery with dynamiccreated textbox not working RRS feed

  • Question

  • User1480781148 posted

    Dear Friends,

    I am creating dynamic textbox's and values of textbox's sumup on one field , but jquery not working my code as followed please help me.

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function () {

    //iterate through each textboxes and add keyup
    //handler to trigger sum event
    $(".txt").each(function () {

    $(this).keyup(function () {
    calculateSum();
    });
    });

    });

    function calculateSum() {

    var sum = 0;
    //iterate through each textboxes and add the values
    $(".txt").each(function () {

    //add only if the value is number
    if (!isNaN(this.value) && this.value.length != 0) {
    sum += parseFloat(this.value);
    }

    });
    //Assign the total value to Textbox 11
    $("#TextBox11").val(sum);
    }
    </script>
    <body>
    <form id="form1" runat="server">
    <div>

    <asp:PlaceHolder ID="myPlaceHolder" runat="server"></asp:PlaceHolder>

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    </div>
    </form>
    </body>
    </html>

    Shared myCount As Integer = 10
    Private dynamicTextBoxes() As TextBox
    Protected Overrides Sub OnInit(ByVal e As EventArgs)
    MyBase.OnInit(e)
    Dim xloc As Integer = 150
    dynamicTextBoxes = New TextBox(myCount - 1) {}
    Dim i As Integer
    For i = 0 To myCount - 1 Step i + 1
    xloc = xloc + 23

    Dim account As TextBox = New TextBox()
    Dim Account_name As TextBox = New TextBox()

    Dim dr As TextBox = New TextBox()
    Dim cr As TextBox = New TextBox()

    dr.ID = "DR" + i.ToString()
    dynamicTextBoxes(i) = dr
    dr.Style("Position") = "Absolute"
    dr.Style("Top") = (xloc).ToString + "px"
    dr.Style("Left") = "640px"
    dr.CssClass = "txt"

    myPlaceHolder.Controls.Add(dr)

    Dim literalBreak As LiteralControl = New LiteralControl("<br />")

    myPlaceHolder.Controls.Add(literalBreak)
    Next
    End Sub

    thank u

    regards ,

    asad

    Wednesday, March 23, 2016 1:44 PM

Answers

  • User-286291038 posted

    Hi Asad,

    You just have to make one small change. In the following code in your javascript, update the textbox name to TextBox1 (that is the id of the static text box you have provided in your html) :)

    //Assign the total value to Textbox 11   
    $("#TextBox1").val(sum);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 23, 2016 2:18 PM