Get all id paragraphs on html page

I have three paragraphs <p>and I want to show their value idin textarea, and this is the code I used:

var pNodesList = document.getElementsByTagName('p');
var text = "";

var pArrayList = Array.prototype.slice.call(pNodesList);

pArrayList.forEach(function(value, index){
    text += value.getAttribute('id') + String.fromCharCode(13);
});

document.getElementsByTagName('textarea')[0].value = text;

The problem is when I click on this button, this is the result that I get in my textarea:

myP1
null
myP2
null
myP3

Why am I getting these null values?

Edit:

This is my HTML:

<p id="myP1" style="font-weight:bold;">
        Ceci est une paragraphe avec deux balises &lt;ul&gt;
        <br>
        <ul>Liste 1 : </ul>
        <ul>Liste 2 : </ul>
    </p>
    <p id="myP2" style="font-weight:bold;">
        Ceci est une paragraphe avec une balise &lt;ul&gt;
        <br>
        <ul>Liste 3 : </ul>
</p>
<p id="myP3" style="font-weight:bold;">Ceci est une paragraphe simple.</p>
+3
source share
2 answers

You probably have paragraphs without an attribute idwhen requested getAttribute, it returns null.

The UPDATE . You put ul inside a paragraph that is prohibited by the HTML specification

+3
source

If you want to avoid printing those that do not have an identifier:

pArrayList.forEach(function(value, index){
  var id = value.getAttribute('id');
  if (id != null)
    text += value.getAttribute('id') + String.fromCharCode(13);
  });
(a == null)
0
source

All Articles