locked
Cannot set property 'innerHTML' of null RRS feed

  • Question

  • User1692641958 posted

    Hello, in my aspx file what I want to do is insert a template in the ul with the id = "id" but I get this error: Cannot set property 'innerHTML' of null , in another aspx file I did it and it worked so it would not be causing this inconvenience

        <script type="text/javascript">
    
             function hola() {
                  var template1 = `
                  <p>d</p>
                 `;
    
                document.getElementById('id').innerHTML = template1;
            }
            hola();
        </script>
    
    <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    
    <div class="t_title"><h4 class="estadistica">ESTADISTICAS MEDICAS</h4></div>
    <ul id="id" class="interna menudoc">
    
    </ul>
    
    </asp:Content>

    Thursday, September 6, 2018 2:37 PM

All replies

  • User475983607 posted

    Move the script below the HTML element.  The script is running before the element exists.  Or wrap the script in the ready event so it fires after the page is loaded in the browser.

    <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    
    	<div class="t_title"><h4 class="estadistica">ESTADISTICAS MEDICAS</h4></div>
    	<ul id="id" class="interna menudoc">
    
    	</ul>
            <script type="text/javascript">
    
             function hola() {
                  var template1 = '<p>d</p>';
    			  document.getElementById('id').innerHTML = template1;
            }
            hola();
        </script>
    </asp:Content>

    Thursday, September 6, 2018 2:47 PM
  • User1692641958 posted

    Try what you told me but it did not come out

    Thursday, September 6, 2018 3:13 PM
  • User475983607 posted

    Try what you told me but it did not come out

    Your response is not specific enough to guess what "it did not come out" means.

    I tested the code and the snippet works as expected.  Meaning the <p>d</p> element is inserted within the <ul> element.  Maybe you really want an <li>?

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link href="content/jquery-ui-1.12.1/jquery-ui.css" rel="stylesheet" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script src="Scripts/jquery-ui-1.12.1.js"></script>
    </head>
    <body>
        <div class="t_title"><h4 class="estadistica">ESTADISTICAS MEDICAS</h4></div>
        <ul id="id" class="interna menudoc"></ul>
        <script type="text/javascript">
    
            function hola() {
                var template1 = '<li>d</li>';
                document.getElementById('id').innerHTML = template1;
            }
            hola();
        </script>
    </body>
    </html>

    Thursday, September 6, 2018 3:39 PM