All,
I am trying to dynamically generate a list of checkboxes and textboxes associated with each other.
On the client side I want to dynamically enable the text boxes when the corresponding checkbox is selected.
C# code to generate the checkboxes and text boxes
foreach (DataRow row in AccountsDS.Tables["Table"].Rows)
{
CheckBox c1 = new CheckBox();
c1.ID = "chk" + row["Num"];
c1.Text = row["Num"].ToString();
c1.CssClass = "AccountSelectBox";
c1.ClientIDMode = System.Web.UI.ClientIDMode.Static;
TextBox t1 = new TextBox();
t1.ID = "txt" + row["Num"];
t1.CssClass = "AccountAmountBox";
t1.Enabled = false;
t1.Width = new Unit("50px");
t1.ClientIDMode = System.Web.UI.ClientIDMode.Static;
TableRow tr = new TableRow();
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
TableCell tc3 = new TableCell();
TableCell tc4 = new TableCell();
TableCell tc5 = new TableCell();
tc1.Controls.Add(c1);
tc2.Controls.Add(t1);
tc3.Text = row["Num"].ToString();
tc4.Text = row["Num1"].ToString();
tc5.Text = row["Num2"].ToString();
tr.Cells.Add(tc1);
tr.Cells.Add(tc3);
tr.Cells.Add(tc4);
tr.Cells.Add(tc5);
tr.Cells.Add(tc2);
AccountsTable.Rows.Add(tr);
}
Client Side script to enable textboxes
$(document).ready(function () {
$(".AccountSelectBox").change(function () {
var index = 0;
$(".AccountSelectBox").each(function () {
alert($(this).attr('checked'));
});
});
});
The checked always gives me "false". Is this the right way to do it? I am trying to look for all the checked fields and use their ID to enable textboxes with the same ID. (both have different prefix making them unique)
Is there a better (or simpler) way to do this?
EDIT:
using $(this).attr('id') also gives me 'undefined'. And trying $(this).val gives a long text of HTML which seems completely irrelevant.
Dynamically Generated HTML
<table id="ContentPlaceHolder1_AccountsTable" style="width:100%;margin-top:20px; text-align:center;">
<tr>
<td><span class="AccountSelectBox"><input id="chk7617537" type="checkbox" name="ctl00$ContentPlaceHolder1$chk7617537" /><label for="chk7617537">7617537</label></span></td><td>7617537</td><td>03/22/2014</td><td></td><td><input name="ctl00$ContentPlaceHolder1$txt7617537" type="text" id="txt7617537" disabled="disabled" class="aspNetDisabled AccountAmountBox" style="width:50px;" /></td>
</tr><tr>
<td><span class="AccountSelectBox"><input id="chk7685249" type="checkbox" name="ctl00$ContentPlaceHolder1$chk7685249" /><label for="chk7685249">7685249</label></span></td><td>7685249</td><td>04/23/2014</td><td></td><td><input name="ctl00$ContentPlaceHolder1$txt7685249" type="text" id="txt7685249" disabled="disabled" class="aspNetDisabled AccountAmountBox" style="width:50px;" /></td>
</tr><tr>
<td><span class="AccountSelectBox"><input id="chk27979714" type="checkbox" name="ctl00$ContentPlaceHolder1$chk27979714" /><label for="chk27979714">27979714</label></span></td><td>27979714</td><td>04/18/2014</td><td></td><td><input name="ctl00$ContentPlaceHolder1$txt27979714" type="text" id="txt27979714" disabled="disabled" class="aspNetDisabled AccountAmountBox" style="width:50px;" /></td>
</tr><tr>
<td><span class="AccountSelectBox"><input id="chk7701997" type="checkbox" name="ctl00$ContentPlaceHolder1$chk7701997" /><label for="chk7701997">7701997</label></span></td><td>7701997</td><td>05/07/2014</td><td></td><td><input name="ctl00$ContentPlaceHolder1$txt7701997" type="text" id="txt7701997" disabled="disabled" class="aspNetDisabled AccountAmountBox" style="width:50px;" /></td>
</tr><tr>
<td><span class="AccountSelectBox"><input id="chk28183452" type="checkbox" name="ctl00$ContentPlaceHolder1$chk28183452" /><label for="chk28183452">28183452</label></span></td><td>28183452</td><td>05/07/2014</td><td></td><td><input name="ctl00$ContentPlaceHolder1$txt28183452" type="text" id="txt28183452" disabled="disabled" class="aspNetDisabled AccountAmountBox" style="width:50px;" /></td>
</tr>
</table>
This is what I get when I use $(this).val

EDIT Reading from textbox
Tried
$(".AccountAmountBox :input").blur(function () {
alert($(this).text);
});
$(".AccountAmountBox").blur(function () {
alert($(this).val);
});