Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
Targeting a <p> and converting it into an array

I would like to target all the <p> and convert it into an array. I have tried spread operator, Array.from or by using methods like .split() or .splice(). But it seems that the problem is to find the right target and understand how to operate with Element object-text which I have not yet...

const targetDates = document.querySelector('.data');
console.log(targetDates.childNodes.entries)
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <p id="dates" class="data">
            1429
    1368
    1661
    1687
    1593
    1495
    1565
    1500
    1635
        </p>
        <script src="app.js"></script>
    </body>
    </html>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

That element has a single child node, which is a text node.

const p = document.querySelector('#dates');
console.log(p.childNodes.length);
        <p id="dates" class="data">
            1429
    1368
    1661
    1687
    1593
    1495
    1565
    1500
    1635
        </p>

In such a case, it's usually easiest to use .textContent to retrieve only the text inside the element, and then you can manipulate the text to process the numbers as you like - such as splitting by newlines, or by matching numbers in the string with a regular expression:

const p = document.querySelector('#dates');
const nums = p.textContent.match(/\d+/g);
for (const num of nums)  {
  console.log(num);
}
<p id="dates" class="data">
            1429
    1368
    1661
    1687
    1593
    1495
    1565
    1500
    1635
        </p>

about 4 years ago · Juan Pablo Isaza Report

0

  const targetDates = document.querySelector('.data');
  const textData=targetDates.childNodes[0].textContent;
  const finalTextData=textData.trim().split('\n');//using trim() because the of the first blank space
  
  const newValue=finalTextData.map(item=>item.trim())

So with the .textcontent you can access to the content of <p> then you have to remove the blank space at first via trim() and split them base of '\n' which is Enter.Although you can find some RegExp to solve the problem ,you can also solve this issue with this steps

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!