So Simple but doesn't work

Joined
Jan 6, 2022
Messages
20
Reaction score
0
This is driving me crazy. Seems like it should alert with the innerHTML from each <p> element, but it doesn't.
Code:
<script>
    function doit() {
    items=document.getElementsByTagName("p");
        for(var i=0; i<items.length; i++);
        {
        alert(items[i].innerHTML);
        }//end for loop     
    }//end foo

</script>

<p>abc</p>
<p>xyz</p>
<button onclick="doit()"> CLICK </button>
 
Joined
Jul 4, 2023
Messages
482
Reaction score
61
Removed semicolon after the for loop declaration. Without the semicolon, the block of code within {} will be executed for each element. ;)

for (var i=0; i<items.length; i++);
HTML:
<script>
  function doit() {
    items = document.getElementsByTagName("p");
    for (var i=0; i<items.length; i++) {
      alert(items[i].innerHTML);
    } //end for loop  
  } //end foo
</script>

<p>abc</p>
<p>xyz</p>
<button onclick="doit()"> CLICK </button>


BTW,
HTML:
<script>
  function doit() {
    const items = document.getElementsByTagName("p");
    for (const item of items) {
      alert(item.textContent);
    }  
  }
</script>

<p>abc</p>
<p>xyz</p>
<button onclick="doit()"> CLICK </button>

HTML:
<script>
  function doit() {
    const items = document.querySelectorAll('p');
    for (const item of items) {
      if (item.innerText === item.innerHTML)
        alert(item.textContent); //This element contains only text
      else
        alert(item.textContent + '\n' + item.innerHTML); // This element contains tags and text
    }   
  }
</script>

<p>abc</p>
<p>xyz<span>abc</span></p>
<p><b>abc</b>def</p>
<p>xyzXYZ</p>
<button onclick="doit()">CLICK</button>
 
Last edited:
Joined
Jan 6, 2022
Messages
20
Reaction score
0
Removed semicolon after the for loop declaration. Without the semicolon, the block of code within {} will be executed for each element. ;)

for (var i=0; i<items.length; i++);
HTML:
<script>
  function doit() {
    items = document.getElementsByTagName("p");
    for (var i=0; i<items.length; i++) {
      alert(items[i].innerHTML);
    } //end for loop 
  } //end foo
</script>

<p>abc</p>
<p>xyz</p>
<button onclick="doit()"> CLICK </button>


BTW,
HTML:
<script>
  function doit() {
    const items = document.getElementsByTagName("p");
    for (const item of items) {
      alert(item.textContent);
    } 
  }
</script>

<p>abc</p>
<p>xyz</p>
<button onclick="doit()"> CLICK </button>

HTML:
<script>
  function doit() {
    const items = document.querySelectorAll('p');
    for (const item of items) {
      if (item.innerText === item.innerHTML)
        alert(item.textContent); //This element contains only text
      else
        alert(item.textContent + '\n' + item.innerHTML); // This element contains tags and text
    }  
  }
</script>

<p>abc</p>
<p>xyz<span>abc</span></p>
<p><b>abc</b>def</p>
<p>xyzXYZ</p>
<button onclick="doit()">CLICK</button>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top