title: Using ClientID to get value from textbox does not work with JavaScript
body: <p>I have seen a few examples about how to get the value from a textbox with JavaScript when the control is on a page that uses a MasterPage. But, it is not working for me.</p>
  
  <p>Here is my JavaScript code:</p>
  
  <pre><code>function changeRedTextToBlackMasterPage()
  {
  // Get the value for the textboxes:
  var userNameEntry = document.getElementById('&lt;%= UserName_tbx.ClientID %&gt;').value;
  </code></pre>
  
  <p>NOTE: The function is in a separate JavaScript file.</p>
  
  <p>Here is the page source:</p>
  
  <pre><code>&lt;input name="ctl00$ContentsPlaceHolder1$UserName_tbx" type="text" value="ww" maxlength="20" id="ctl00_ContentsPlaceHolder1_UserName_tbx" onblur="changeRedTextToBlackMasterPage()" style="width:150px;" /&gt;
  </code></pre>
  
  <p>Here is the error I am getting:</p>
  
  <blockquote>
    <p>SCRIPT5007: Unable to get value of the property 'value': object is
    null or undefined</p>
  </blockquote>
  
  <p>If I use the ID in the generated page in the JavaScript function, the code works.</p>
  
  <pre><code>var userNameEntry = document.getElementById('ctl00_ContentsPlaceHolder1_UserName_tbx').value;
  </code></pre>
  
  <p>So, why didn't using the ClientID work in this case?
  The textbox is in a table. But, I don't think that would make a difference.
  (Did I make a typo in the code that I am not seeing? I tried copying/pasting the examples into my code to compare them.)</p>
  
  <p>Here is the rest of the code from the page up to the point where the textbox is defined:</p>
  
  <pre><code>&lt;%@ Page Language="C#" MasterPageFile="~/RaptorNestSurveyMaster01.master" AutoEventWireup="true" CodeFile="EditUserInformation.aspx.cs" Inherits="EditUserInformation" Title="Edit User Information Page" %&gt;
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="ContentsPlaceHolder1" Runat="Server"&gt;
  
  &lt;div&gt;
  &lt;asp:label ID="PageTitle_lbl" runat="server" CssClass="pageTitle" text="Edit User Information Page" /&gt;
  &lt;br /&gt; &lt;br /&gt; &lt;br /&gt; 
    &lt;table&gt;
      &lt;tr&gt;
         &lt;td&gt;
           &lt;asp:Label ID="UserName_lbl" runat="server" Text="User Name:"&gt;&lt;/asp:Label&gt; 
         &lt;/td&gt;
         &lt;td style="width: 745px"&gt;
           &lt;asp:TextBox ID="UserName_tbx" runat="server" MaxLength="20" Width="150" onblur="changeRedTextToBlackMasterPage()" &gt;&lt;/asp:TextBox&gt;&amp;nbsp;
           &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="UserName_tbx" ErrorMessage="User Name is required."&gt;&lt;/asp:RequiredFieldValidator&gt;
           &lt;asp:Label ID="UserNameErrorMessage_lbl" runat="server" ForeColor="Red" Text="" Visible="false"&gt;&lt;/asp:Label&gt;
         &lt;/td&gt; 
       &lt;/tr&gt;    
  </code></pre>