title: Javascript Runtime Error: Unable to get property 'style' of undefined or null reference of dynamically created Textboxes
body: <p>I'm creating some textboxes in <code>ASP.NET C#</code> Code-Behind and trying to style them using a simple <code>javascript</code>, which I also want to call from Code-Behind. </p>
  
  <p>The Problem is that I'm getting a <code>Javascript</code> Runtime Error. The error is <code>"0x800a138f - JavaScript runtime error: Unable to get property 'style' of undefined or null reference"</code></p>
  
  <h3>My ".aspx" page is as follows:</h3>
  
  <pre><code>&lt;head runat="server"&gt;
    &lt;title&gt;&lt;/title&gt;
  
    &lt;script type="text/javascript"&gt;
  
      function call_me(x) {
        var txtbox = document.getElementById(x);
        txtbox.style.backgroundColor = 'black';
        txtbox.style.color = 'yellow';
      }
  
    &lt;/script&gt;
  
  &lt;/head&gt; 
  
  &lt;body&gt;
    &lt;form id="form1" runat="server"&gt;
  
      &lt;div&gt;
        &lt;asp:Panel ID="Panel1" runat="server"&gt;&lt;/asp:Panel&gt;
      &lt;/div&gt;
  
    &lt;/form&gt;
  &lt;/body&gt;
  </code></pre>
  
  <h3>My "C# Code-Behind" file is as follows:</h3>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e) {
  
    for (int i = 0; i &lt; 5; i++) {
  
      //textbox creation
      TextBox tx = new TextBox();
      tx.ID = "textbox" + i.ToString();
      string client_id = tx.ClientID;
      tx.Text = client_id;
  
      //script and key creation
      string key_for_javascript = "call" + i.ToString();
      string script_to_load = "call_me('" + client_id + "');";
  
      //add textbox to panel
      Panel1.Controls.Add(tx);
  
      //call script
      ScriptManager.RegisterClientScriptBlock(this, this.GetType(), key_for_javascript, script_to_load, true);
  
    }
  
  }
  </code></pre>
  
  <p>I'm not sure where I am going wrong or how I should fix this. Please help me! </p>