How do you format a textbox inside a gridview using javascript. Here's what my gridview columns looks like:
<Columns>
<asp:TemplateField>
<ItemTemplate><asp:ImageButton runat="server" id="imgbtnEnterAmt" ImageUrl="~/Images/bullet_list.png" Width="12px" Height="12px" OnClick="imgbtnEnterAmt_Click" /> </ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="particulars" HeaderText="HOSPITAL CHARGES/ PARTICULARS" />
<asp:TemplateField HeaderText="ACTUAL">
<%--<ItemTemplate><asp:TextBox runat="server" ID="txt_ipamt" OnTextChanged="txt_ipamt_TextChanged" AutoPostBack="true"></asp:TextBox> </ItemTemplate> --%>
<ItemTemplate><input type="text" id="txth_ipamt" onblur="format_amt()" /> </ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="deductions" HeaderText="DEDUCTION/ PHILHEALTH" />
</Columns>
And my javascript on formatting the textbox:
function format_amt()
{
var str_amt = document.getElementById('txth_ipamt').value;
var n = parseFloat(str_amt).toFixed(2);
document.getElementById('txth_ipamt').value = n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
The problem is, when I tend to add more rows, only the first textbox is formatted upon onblur and when the onblur event is triggered again the textbox gets formatted again resulting in a wrong output.