title: Javascript to disable inline onclick for element loaded as asp.net control
body: <p>I have an ASP.NET Webforms page. In the code-behind, an <code>onclick</code> attribute is added to a label control and a textbox control using the code -</p>
  
  <pre><code>lblName.Attributes.Add("onClick", "javascript:location.href='NameForm.aspx?id=" &amp; CStr(fnRetRow("name_id")) &amp; "';")
  txtName.Attributes.Add("onClick", "javascript:location.href='NameForm.aspx?id=" &amp; CStr(fnRetRow("name_id")) &amp; "';")
  </code></pre>
  
  <p>The following HTML elements are generated. (Note, these are ultimately located within several nested layers of <code>&lt;table&gt;&lt;tr&gt;</code>... elements on the page.) -</p>
  
  <pre><code>&lt;td class="formlabel"&gt;
    &lt;span id="lblName" class="formlabel" onclick="javascript:location.href='NameForm.aspx?id=123';"&gt;&lt;/span&gt;
  &lt;/td&gt;
  &lt;td class="forminputtxt"&gt;
    &lt;input id="txtName" class="forminputtxt" onclick="javascript:location.href='NameForm.aspx?id=123';"&gt;
  &lt;/td&gt;
  </code></pre>
  
  <p><strong>QUESTION:</strong> How can I use a simple JavaScript function in the aspx page to disable these <code>onclick</code> attributes after the page loads (or elements loaded) ?</p>
  
  <p>I've tried the following options without success -</p>
  
  <pre><code>&lt;script language = "javascript"&gt;
    window.onload = function() {
      document.getElementById('lblName').onclick = '';    // option 1 for label
      document.getElementById('txtName').onclick = '';    // option 1 for textbox
  
      document.getElementById('lblName').removeAttribute("onclick");  //option 2 for label
      document.getElementById('txtName').removeAttribute("onclick");  // option 2 for textbox
    }
  &lt;/script&gt;
  </code></pre>
  
  <p>Option 2 has no effect. Option 1 for the textbox has no effect.</p>
  
  <p>Option 1 for the label does somehow disable the link; however, the inline javascript is still displayed in the final HTML of the page. This is acceptable, but not ideal.</p>
  
  <p>What is a simple way to disable (or ideally remove) both <code>onclick</code> attributes after page load?</p>
  
  <p>NOTE: JQuery is not available in the page.</p>