Reputation: 7074
ASP.NET is auto generating an ID instead of using the one I assign. How would I select this value using jQuery? I am not trusting that the auto generated value would always be the same.
Here is my ASPX Code:
<asp:TextBox ID="txtZip" runat="server" CausesValidation="true" CssClass="short_width" MaxLength="10" />
Here is what is generated:
<input name="ctl00$MainContentArea$txtZip" type="text" maxlength="10" id="ctl00_MainContentArea_txtZip" class="short_width" />
Here is what I am trying to do:
<script type="text/javascript">
jQuery(function ($) {
$("#txtZip").mask("99999?-9999");
});
</script>
Upvotes: 3
Views: 850
Reputation: 8584
.Net 4.5 has ClientIDMode property on controls, if you set it to static then the control's ID won't change from what you assign to it, it'll stay as txtZip.
Upvotes: 0
Reputation: 8941
If your javascript is in the aspx
page you can use ClientId
. Otherewise you'll have to use a class like in the other answer.
<script type="text/javascript">
jQuery(function ($) {
$("#<%=txtZip.ClientId%>").mask("99999?-9999");
});
</script>
Upvotes: 4
Reputation: 33326
You can use the class selector:
jQuery(function ($) {
$(".short_width").mask("99999?-9999");
});
I would create a new class i.e. zipCode and add it to your control:
<asp:TextBox ID="txtZip" runat="server" CausesValidation="true" CssClass="short_width zipCode" MaxLength="10" />
Then use this:
jQuery(function ($) {
$(".zipCode").mask("99999?-9999");
});
That way it keeps it separate from your other styles.
Upvotes: 5