Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i am creating textboxes dynamically so how to call below javascript function for textbox 'onchange' event?
i want to calculate two dynamically created textboxes values.

XML
<script type="text/javascript">
    debugger;
    function myFunction() {
        var btn = document.getElementById('<%= temp.ClientID%>').value;
        var btntemp = document.getElementById('<%= txttemp2.ClientID%>').value;
        var val = parseInt(btn) + parseInt(btntemp);
        document.getElementById('<%= TextBox1.ClientID%>').value = val;
    }
</script>
<asp:TextBox ID="temp" runat="server" onchange="myFunction()"></asp:TextBox>
   <asp:TextBox ID="txttemp2" runat="server" onchange="myFunction()"></asp:TextBox>
Posted

1 solution

You can set it as attribute
txt_box.Attributes.Add("onchange", "myFunction(this);");

Also change your function to accept text box as parameter
JavaScript
function myFunction(control) {
        alert(control.id);
    }


if you need to get sum of textbox values, you can set class name for textboxes from codebehind as below

protected void Button1_Click(object sender, EventArgs e)
{
    Table table = new Table();
    table.ID = "Table1";
    PlaceHolder1.Controls.Add(table);
    for (int i = 0; i < 4; i++)
    {
        TableRow row = new TableRow();
        row.ID = "Row_" + i;
        TableCell cell = new TableCell();
        TextBox tb = new TextBox();
        tb.ID = "TextBoxRow_" + i ;
        tb.CssClass = "sum";
        cell.Controls.Add(tb);
        tb.Attributes.Add("onchange", "myFunction();");
        row.Cells.Add(cell);
        table.Rows.Add(row);
    }
}

then you can use find control by class name and calculate the sum as below

JavaScript
function myFunction() {
    var sum = 0;
    var elements = document.getElementsByClassName("sum");
    for (var i = 0, length = elements.length; i < length; i++) {
        if (elements[i].value) {
            sum = sum + (parseInt(elements[i].value) || 0);
        }
    }
    document.getElementById('<%= TextBox1.ClientID%>').value = sum;
}
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 2-Jun-15 1:50am    
5!
DamithSL 2-Jun-15 1:53am    
Thank you, Karthik!
DamithSL 2-Jun-15 2:45am    
<pre>tb.Attributes.Add("onchange", "myFunction(this);");
//add above line before below line in your code..
cell.Controls.Add(tb);<pre>
Asma Siddiqua 2-Jun-15 2:43am    
see i did like below but how to get these three ids into function?

else if (j < colsCount - 1)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox();
if (j == 2)
{
tb.ID = "TextBoxRow_" + i + "Col_" + j;
tb.Attributes.Add("onchange", "myFunction(this);");
}
if (j == 3)
{
tb.ID = "TextBoxRow_" + i + "Col_" + j;
tb.Attributes.Add("onchange", "myFunction(this);");
}
if (j == 4)
{
tb.ID = "TextBoxRow_" + i + "Col_" + j;
tb.Attributes.Add("onchange", "myFunction(this);");
}
if (j == 5)
{
tb.ID = "TextBoxRow_" + i + "Col_" + j;
}
cell.Controls.Add(tb);
row.Cells.Add(cell);
}
Asma Siddiqua 2-Jun-15 2:49am    
but how to get three ids in jsfunction?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900