Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
Limit number of characters inside string containing HTML using Javascript

I have a string that looks like the below. I'd like a function in Javascript that would take such a string as a parameter, and number of real characters (ex: 20), and return the final HTML with the limited characters.

<div>
<p>This is line 1</p>
<p>This is the second line</p>
</div>

So for the above, it should return:

<div>
    <p>This is line 1</p>
    <p>This i</p>
</div>

I understand this may be too complicated to do, but i basically want to take an HTML string and ultimately render it with a specified limit of characters. If there is any other way that would also be good. Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can do it with the .substring() Function

const numOfChars = 5 - 1; // Five is the Input you get
const pElem = document.querySelectorAll('p');
pElem[1].innerText = pElem[0].innerText.substring(0,numOfChars);
<div>
<p>This is line 1</p>
<p>This is the second line</p>
</div>

Be sure to subtract 1 from the amount of desired characters, because the substring Function starts counting at 0

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda