-1
    function change(){
      var a = document.getElementsByTagName("p");
      for(var x = 0 ; x < a.length ; x++){
      a[x].innerHTML = "hello" ;}}
      change(); 

I just want to change content of all p tags , but I can't call this function , I know it will be working if I add window.onload = this function . But I don't know why I can't use the way above to call this function. Pls explain for me .

Bussller
  • 1,639
  • 3
  • 31
  • 36
  • 3
    You *can* use that code, but you have to make sure the code runs only after the DOM is ready. Try moving the ` – Pointy Feb 27 '19 at 15:13

1 Answers1

1

You script must be executed after page chargement, to do this, you must just add your js code after your html code: like below:

<html>
<head>
</head>
<body>
<p>
sdsdsd
</p>

<div>
<p>
hesdsd sdsdsd
</p>
</div>
<script >
function change(){
      var a = document.getElementsByTagName("p");
      for(var x = 0 ; x < a.length ; x++){
      a[x].innerHTML = "hello" ;}}
      change(); 
</script>

</body>
</html>
s.grabou
  • 9
  • 3