title: Get element by id in javascript without having any form tag on page
body: <p>I am wondering if it is possible to get the element by id in javascript when there is no form tag available.</p>
  
  <p>I am using the following command to get the text box value but i am getting error message 'null or undefined'. This works fine if i use proper form tag on asp.net page. </p>
  
  <pre><code>var _txt = document.getElementById("txt").value;
  </code></pre>
  
  <p>Is there any other way of getting textbox values in javascript (when there is no form,body and header tag in asp.net page)</p>
  
  <p>Here is the code</p>
  
  <pre><code>enter code here
  </code></pre>
  
  <p></p>
  
  <pre><code>    &lt;asp:dropdownlist runat="server" ID="ddl" Height="20px" 
          Width="215px" ValidationGroup="aa"&gt;
          &lt;asp:ListItem Selected="True" Value="0"&gt;--select--&lt;/asp:ListItem&gt;
          &lt;asp:ListItem Value="1"&gt;item-1&lt;/asp:ListItem&gt;
      &lt;/asp:dropdownlist&gt;
  
      &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
          ControlToValidate="ddl" ErrorMessage="required" InitialValue="0" 
          SetFocusOnError="True" Display="Dynamic" ValidationGroup="aa"&gt;&lt;/asp:RequiredFieldValidator&gt;
  
      &lt;br /&gt;
      &lt;asp:textbox runat="server" ID="txt" ValidationGroup="aa" Width="140px"&gt;&lt;/asp:textbox&gt;
      &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
          ControlToValidate="txt" ErrorMessage="required" InitialValue="" 
          SetFocusOnError="True" Display="Dynamic" ValidationGroup="aa"&gt;&lt;/asp:RequiredFieldValidator&gt;
      &lt;br /&gt;
  
  &lt;/div&gt;
  
  &lt;div&gt;
      &lt;asp:validationsummary runat="server" ID="valSummary"
          HeaderText="Vendor Ref and Contact are required" ValidationGroup="aa" 
          CssClass="valsummary"&gt;
      &lt;/asp:validationsummary&gt;
  &lt;/div&gt;
  
  
      &lt;asp:Button ID="btn" runat="server" Text="DO" ValidationGroup="aa" 
      Width="118px" OnClientClick="Test2()"/&gt;
  
  
  &lt;br /&gt;
  
  
  &lt;script type="text/javascript" language="javascript"&gt;
  
  
      function Test() {
          var _indx = document.getElementById('ddl');
          var _txt = document.getElementById("txt").value;
  
          if (_indx.selectedIndex == 0 || _txt == '') {
              document.getElementById('_div').style.visibility = 'visible';
              document.getElementById('_div').style.display = "block";
          }
          else {
  
              document.getElementById('_div').style.visibility = 'hidden';
              document.getElementById('_div').style.display = "none";
          }
  
      }
  
      function Test2() {
          var _indx = document.getElementById('ddl');
          var _txt = document.getElementById("txt").value;
          var msg_I = "........"
          var msg_II = "...."
          var msg_III = ".."
  
  
          if (_indx.selectedIndex == 0 &amp;&amp; _txt == '') {
              txtHeader = msg_I
          }
  
          else if (_indx.selectedIndex == 0 &amp;&amp; _txt != '') {
  
              txtHeader = msg_III
          }
  
          else if (_indx.selectedIndex != 0 &amp;&amp; _txt == '') {
  
              txtHeader = msg_II
          }
  
          document.getElementById('&lt;%= valSummary.ClientID %&gt;').headertext = txtHeader;
      }  &lt;/script&gt;
  </code></pre>