Why the code innerHTML on javascript is not working?

yseol :

there are HTML code and JavaScript code

<script>
   function Stock_Listing() {
      document.getElementById("stock_menu").innerHTML = "hi";
   }
</script>

<ul id="stock_menu" onload="Stock_Listing()"></ul>

but there is nothing on my page.

could you plz tell me why is not working?

and also not working insertAdjacentHTML.

<script>
    function Stock_Listing() {
       document.getElementById("stock_menu").insertAdjacentHTML = ("afterbegin", "hi");
    }
</script>

<ul id="stock_menu" onload="Stock_Listing()"></ul>
Nicolas :

In your onLoad function, you are declaring a function but never calling it. You don't need that second function:

window.onload = function() {
  document.getElementById("stock_menu").innerHTML = "hi";
}
    <ul id="stock_menu"></ul>

Or at least, you need to call that second function:

   window.onload = function() {
      function Stock_Listing() {
         document.getElementById("stock_menu").innerHTML = "hi";
      }
      
      Stock_Listing();
   }
<ul id="stock_menu"></ul>

Both method works, I would prefer the first one since you don't really need the Stock_Listing function.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=342087&siteId=1