title: Getting Error On Javascript document.getElementById('name'). RETURNS NULL
body: <p>I need to execute a javascript function on ASP.NET, but I've encountered an error that I can't seem to fix...</p>
  
  <p>I have alreay tried a small example that works...
  here it is...</p>
  
  <p>Aspx file</p>
  
  <pre><code>&lt;title&gt;Untitled Page&lt;/title&gt;
  &lt;script type="text/javascript" language="javascript"&gt;
      function ExecuteConfirm() {
      debugger
          var returnValue = confirm('Do you want to proceed further?');
          if (returnValue) {
              document.getElementById('hdnExecuteAfterConfirm').value = "1";
              document.getElementById('btnClickMe').click();
          }
  
  
      }
  &lt;/script&gt;
  
  
  &lt;body&gt;
  &lt;form id="form1" runat="server"&gt;
  &lt;asp:Label runat="server" ID="lblMessage"&gt;
  &lt;/asp:Label&gt;
  
  &lt;div&gt;
      &lt;asp:Button ID="btnClickMe" runat="server" Text="Click me" 
          onclick="btnClickMe_Click" /&gt;
  &lt;/div&gt;
  &lt;asp:HiddenField runat="server" ID="hdnExecuteAfterConfirm" /&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>CS file</p>
  
  <pre><code>protected void btnClickMe_Click(object sender, EventArgs e)
      {
          //Do server side processing before confirmation
          if (hdnExecuteAfterConfirm.Value != "1")
          {
              lblMessage.Text = "You clicked the button";
              ClientScript.RegisterStartupScript(this.Page.GetType(), btnClickMe.ID, "ExecuteConfirm()", true);
  
          }
          //Do server side processing after confirmation
          else
          {
              //Proceed further with server side processing
  
              //Reset the value of hidden field
              hdnExecuteAfterConfirm.Value = "";
              //Notify the user that processing is complete.
              ClientScript.RegisterStartupScript(this.Page.GetType(), btnClickMe.ID, "alert('Processing is complete.')", true);
          }
      }
  </code></pre>
  
  <p>The goal here is to display a confirm message the first time you click the button (if), and once you click YES on the confirm message it will repeat the button function, however, it'll do something different (else)...</p>
  
  <p>I'm trying to recreate this function on my project, but I am encountering an error...</p>
  
  <p>Here's what I have...</p>
  
  <p>Note, I'm not copying my entire project, those are just the parts that I had to change...</p>
  
  <p>Site.Master</p>
  
  <pre><code>&lt;script type="text/javascript" language="javascript"&gt;
          function ConfirmarRegistroFecha(strAdvertencia) {
              debugger
              var returnValue = strAdvertencia;
  
              if (returnValue) {
                  try{
                  document.getElementById('hdnExecuteAfterConfirm').value = "1";  
                  document.getElementById('btnGuardarEvento').click();
                  }
  
                  catch(error){
                      alert(error);
                  }
              }
          }
      &lt;/script&gt;
  </code></pre>
  
  <p>Agenda.aspx (I know that is to much, however, I'm copying it all, since maybe the error is around here somewhere. The point is that at least, inside is a HiddenField, and a button) </p>
  
  <pre><code>&lt;asp:View ID="ViewOperacionesAgenda2" runat="server"&gt;
              &lt;asp:HiddenField runat="server" ID="hdnExecuteAfterConfirm" /&gt;
              &lt;br /&gt;
  
              &lt;asp:Panel ID="pnlRegistrarEvento" runat="server" DefaultButton="btnGuardarEvento"&gt;
                  &lt;table&gt;
                      &lt;tr&gt;
                          &lt;td align="left"&gt;
                              Seleccione Vendedor:
                              &lt;br /&gt;
  
                              &lt;asp:DropDownList ID="dpdLstVendedoresAgenda" runat="server" Width="150px"&gt;
                              &lt;/asp:DropDownList&gt;
                          &lt;/td&gt;
  
                          &lt;td align="left" rowspan="3"&gt;
                              Descripcion:
                              &lt;br /&gt;
  
                              &lt;asp:TextBox ID="tbxAlAgendaDescripcion" runat="server" Height="150px" TextMode="MultiLine" ValidationGroup="gpAgendaAlta" Width="150px"&gt;
                              &lt;/asp:TextBox&gt;
                          &lt;/td&gt;
  
                          &lt;td align="left" rowspan="3"&gt;
                              Fecha:
                              &lt;br /&gt;
  
                              &lt;asp:Calendar ID="Calendar1" runat="server" BackColor="White" 
                                  BorderColor="#3366CC" BorderWidth="1px" CellPadding="1" 
                                  DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="7pt" 
                                  ForeColor="#003399" Height="150px" Width="150px"&gt;
                                  &lt;DayHeaderStyle BackColor="#99CCCC" ForeColor="#336666" Height="1px" /&gt;
                                  &lt;NextPrevStyle Font-Size="8pt" ForeColor="#CCCCFF" /&gt;
                                  &lt;OtherMonthDayStyle ForeColor="#999999" /&gt;
                                  &lt;SelectedDayStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" /&gt;
                                  &lt;SelectorStyle BackColor="#99CCCC" ForeColor="#336666" /&gt;
                                  &lt;TitleStyle BackColor="#003399" BorderColor="#3366CC" BorderWidth="1px" Font-Bold="True" Font-Size="10pt" ForeColor="#CCCCFF" Height="25px" /&gt;
                                  &lt;TodayDayStyle BackColor="#99CCCC" ForeColor="White" /&gt;
                                  &lt;WeekendDayStyle BackColor="#CCCCFF" /&gt;
                              &lt;/asp:Calendar&gt;
                          &lt;/td&gt;
  
                          &lt;td rowspan="3"&gt;
                              &lt;table&gt;
                                  &lt;tr&gt;
                                      &lt;td align="left"&gt;
                                          Hora Inicio:
                                          &lt;br /&gt;
  
                                          &lt;asp:TextBox ID="tbxAlAgendaHoraInicio" runat="server" ValidationGroup="gpAgendaAlta" Width="130px"&gt;0:00 am&lt;/asp:TextBox&gt;
  
                                          &lt;br /&gt;
  
                                          &lt;asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" 
                                              ControlToValidate="tbxAlAgendaHoraInicio" 
                                              ErrorMessage="Hora Invalida: use 00:00 pm" 
                                              ValidationExpression="^((0?[1-9]|1[012])(:[0-5]\d)((\ )?[ap](.)?m(.)?))" 
                                              ValidationGroup="vgpRegistrarEvento" Font-Bold="True" ForeColor="Red"&gt;
                                          &lt;/asp:RegularExpressionValidator&gt;
  
                                          &lt;br /&gt;
  
                                          &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" 
                                              ControlToValidate="tbxAlAgendaHoraInicio" ErrorMessage="Campo Requerido" 
                                              ValidationGroup="vgpRegistrarEvento" Font-Bold="True" ForeColor="Red"&gt;
                                          &lt;/asp:RequiredFieldValidator&gt;
                                      &lt;/td&gt;
                                  &lt;/tr&gt;
  
                                  &lt;tr&gt;
                                      &lt;td align="left"&gt;
                                          Hora Final:
                                          &lt;br/&gt;
  
                                          &lt;asp:TextBox ID="tbxAlAgendaHoraFinal" runat="server" ValidationGroup="gpAgendaAlta" Width="130px"&gt;0:00 am&lt;/asp:TextBox&gt;
  
                                          &lt;br /&gt;
  
                                          &lt;asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" 
                                              ControlToValidate="tbxAlAgendaHoraFinal" 
                                              ErrorMessage="Hora Invalida: use 00:00 pm" 
                                              ValidationExpression="^((0?[1-9]|1[012])(:[0-5]\d)((\ )?[ap](.)?m(.)?))" 
                                              ValidationGroup="vgpRegistrarEvento" Font-Bold="True" ForeColor="Red"&gt;
                                          &lt;/asp:RegularExpressionValidator&gt;
  
                                          &lt;br /&gt;
  
                                          &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" 
                                              ControlToValidate="tbxAlAgendaHoraFinal" ErrorMessage="Campo Requerido" 
                                              ValidationGroup="vgpRegistrarEvento" Font-Bold="True" ForeColor="Red"&gt;
                                          &lt;/asp:RequiredFieldValidator&gt;
                                      &lt;/td&gt;
                                  &lt;/tr&gt;
                              &lt;/table&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
  
                      &lt;tr&gt;
                          &lt;td align="left"&gt;
                              Seleccione Cliente:
                              &lt;br /&gt;
  
                              &lt;asp:DropDownList ID="dpdLstClientesAgenda" runat="server" AutoPostBack="True" onselectedindexchanged="dpdLstClientesAgenda_SelectedIndexChanged" Width="150px"&gt;
                              &lt;/asp:DropDownList&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
  
                      &lt;tr&gt;
                          &lt;td align="left"&gt;
                              Tipo:
                              &lt;br /&gt;
  
                              &lt;asp:DropDownList ID="dpdLstAlAgendaTipo" runat="server" Width="150px"&gt;
                                  &lt;asp:ListItem&gt;Reunion&lt;/asp:ListItem&gt;
                                  &lt;asp:ListItem&gt;Entrevista&lt;/asp:ListItem&gt;
                                  &lt;asp:ListItem&gt;Venta&lt;/asp:ListItem&gt;
                                  &lt;asp:ListItem&gt;Soporte&lt;/asp:ListItem&gt;
                                  &lt;asp:ListItem&gt;Instalacion&lt;/asp:ListItem&gt;
                              &lt;/asp:DropDownList&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/table&gt;
  
                  &lt;br /&gt;
                  &lt;asp:Button ID="btnCancelarEvento" runat="server" CausesValidation="False" onclick="btnCancelarEvento_Click" Text="Cancelar" Width="90px" /&gt;
                  &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                  &lt;asp:Button ID="btnGuardarEvento" runat="server" onclick="btnGuardarEvento_Click" Text="Guardar" ValidationGroup="vgpRegistrarEvento" Width="90px" /&gt;
              &lt;/asp:Panel&gt;
  </code></pre>
  
  <p></p>
  
  <p>Agenda.aspx.cs</p>
  
  <pre><code>protected void btnGuardarEvento_Click(object sender, EventArgs e)
      {
  
              if (hdnExecuteAfterConfirm.Value != "1")
              {
                  strAdvertencia = "MESSAGE I WANT TO DISPLAY";
                  ClientScript.RegisterStartupScript(this.Page.GetType(), btnGuardarEvento.ID, "ConfirmarRegistroFecha(" + strAdvertencia + ")", true);
  
              }
  
              else
              {
                  hdnExecuteAfterConfirm.Value = "";
                  //Rest of process
              }
      }
  </code></pre>
  
  <p>Using a debugger with Firebug, I can clearly see that on Site.Master, when it reaches..</p>
  
  <pre><code>document.getElementById('hdnExecuteAfterConfirm').value = "1";
  </code></pre>
  
  <p>It displays the following exception:</p>
  
  <pre><code>TypeError: document.getElementById("hdnExecuteAfterConfirm") is null
  </code></pre>
  
  <p>I have already tried to copy the javascript on Agenda.aspx instead of Site.Master... and it still doesn't work...</p>
  
  <p>Does anyone know what the mistake here is...
  I'm new to javascript, so I'm still believing that is a syntax error...</p>
  
  <p>Thanks in advance and I hope you can help me</p>