gene
gene

Reputation: 2108

bootstrap, jQuery - cannot find the element

I want to get the value of text box to perform validation on it, but somehow my jQuery code gives me an error when trying to retrieve the value from it. I'm using bootstrap as well.

This is the error:

Uncaught TypeError: Cannot read property 'value' of null

This is my function:

$(function () {
    var year = document.getElementById('<%=txtReportYear.ClientID %>').value;
    alert(year);
});

This is the fragment of the code that has the text box:

<div class="form-group">
    <asp:TextBox CssClass="form-control" ID="txtReportYear" runat="server" placeholder="Enter Year" MaxLength="4"></asp:TextBox>
</div>

However, after substituting <%=txtReportYear.ClientID %> with the exact id of the control MainContent_maincontent_txtReportYear, everything works fine.

What am I missing?

Upvotes: 0

Views: 76

Answers (1)

ic3man7019
ic3man7019

Reputation: 711

ASP.NET markup is translated into HTML by the browser. You can set ClientIDMode=Static to force the browser to keep the ID value that you set.

Upvotes: 2

Related Questions