DKR DKR - 29 days ago 5
ASP.NET (C#) Question

document.getelementbyId() returns null even though script is at the end of file

I am trying to execute a JS function which is written at the bottom of .aspx file.

Div tag:

<div id="div_NRContainer" oninit="div_NRContainer_Init" class="panelContainer" runat="server">


after this under script tag:

<script type="text/javascript">

function show_tabs(id) {

var nrcontainer = document.getElementById("div_NRContainer");
alert(nrcontainer);
}

Answer

You are trying to get a server side control. Chances are, that Asp.Net has changed the Id of your element. If your script is on the same aspx page this could should work:

<script type="text/javascript">
     function show_tabs(id) {
         var nrcontainer = document.getElementById("<%= div_NRContainer.ClientID %>");
         alert(nrcontainer);
     }
</script>

Read up on ClientIDMode: https://msdn.microsoft.com/en-US/library/system.web.ui.control.clientidmode(v=vs.110).aspx

Comments