0

I have a simple html that I need to update certain area as text in div. The javascript file is working and the alret function is working but the inner html doesnt work. Note, Im not sure if I need to include the onload function but I was trying without it based on the my research i think it is a must. any suggestion to fix it and advice for best practice and improve the code is appreciated

HTML file

<script type="text/javascript" src="compjavascript.js"></script>

<div class="wrapper">

<div id="sec" onload="fechData();">
 <div class="center"> 
         <div class="column middle"> <p id="text"></p>
             <a href="url" id="link"></a>
         </div>

         <div class="column side">
             <img class="image" id="image" src="download.jpg" >
        </div>
        </div>
       </div>
      </div>

JavaScript File:

alert("javascript");

function fechData() {
document.getElementById("text").innerHTML = "My First JavaScript";
document.getElementById("link").innerHTML = " ";
document.getElementById("image").innerHTML = " ";
}
AbdallahRizk
  • 688
  • 1
  • 6
  • 15

1 Answers1

2

The onload event can only be used on the body

function fechData() {
  document.getElementById("text").innerHTML = "My First JavaScript";
  document.getElementById("link").innerHTML = " ";
  document.getElementById("image").innerHTML = " ";
}
<body onload="fechData();">
  <div class="wrapper">

    <div id="sec">
      <div class="center">
        <div class="column middle">
          <p id="text"></p>
          <a href="url" id="link"></a>
        </div>

        <div class="column side">
          <img class="image" id="image" src="download.jpg">
        </div>
      </div>
    </div>
  </div>
</body>
Nidhin Joseph
  • 8,658
  • 3
  • 18
  • 40