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

205
Views
Truncate text with JavaScript

HI All im looking to make a function to truncate the text based on the value given in data attribute

so far i got nowhere my code below

    <p data-truncate="10">This is a paragraphasdasd asda ascevgr .</p>
<p data-truncate="30">This is a paragrap fdg  heg hasdasd asda ascevgr .</p>
<p data-truncate="12">This is a paragraphasdasd asda ascevgr .</p>
<p data-truncate="14">This is a paragr df fdg 534t34 34 aphasdasd asda ascevgr .</p>
<p data-truncate="16">This is a paragra 34t  gh jy rth phasdasd asda ascevgr .</p>
<p data-truncate="12">This is a paragr 3688 hjhj aphasdasd asda ascevgr .</p>
<p data-truncate="5">This is a paragraph  der ert rt asdasd asda ascevgr .</p>
<p data-truncate="90">This is a paragraph  der ert rt asdasd asda ascevgr .</p>
<script>
    let truncateme = document.querySelectorAll("[data-truncate]");
    truncateme.forEach(truncate);
    function turnecate(item) {
        var value = item.getAttribute('data-truncate');
        var txt = item.textContent;
        if (txt.length > value) {
            var txt1 = item.textContent.substring(0,value) + '...';
            console.log(txt1)
        }else{
            console.log(txt)
        }
        
    }


</script>
about 4 years ago · Juan Pablo Isaza
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!