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

209
Vistas
How to get html of element but ignoring children tags except br tag
<div class="test"> Div Text lorem ipsum <br> lorem ipsum <p class="some_class">Paragraph Content <br> tag and again child nested in it <span> span content</span></p></div>.

I want to get html of div element. Here is tricky part, If i use .html() this will also include children tags which is <p class="some_class">....<span>...</span>...</p> I only need br tag. How can I achieve that.

final output should look like:

Div Text lorem ipsum <br> lorem ipsum Paragraph content <br> tag and again child nested in it span content

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

0

If you want only text present inside any element, you can use innerText but you mentioned you required <br> tag in a result. So you can use innerText first, then replace line feed (\n) with <br> tag.

var divtext = document.getElementsByClassName('test')[0].innerText;
console.log(divtext);
console.log(divtext.replaceAll('\n', '<br>'));
<div class="test"> Div Text lorem ipsum <br> lorem ipsum <p class="some_class">Paragraph Content <br> tag and again child nested in it <span> span content</span></p></div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use a JavaScript (while/for) loop.

Here's a live example.

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Get Children Content with br tag</title>
  </head>
  <body>
   <div id="test"> Div Text lorem ipsum <br> lorem ipsum <p class="some_class">Paragraph Content <br> tag and again child nested in it <span> span content</span></p></div>.
  </body>
  <script>
    //   get all the content of the  div tag
    let test = document.getElementById("test");
    let testData = test.innerHTML;

    let finalText = "";

    // this variable will decide to capture the lettes when captured in loop.
    let capture = true;

    // starting to over all the cahracter in the text content
    for (let i = 0; i < testData.length; i++) {
      let recursive = false;
      if (
        testData.charAt(i) == "<" &&
        testData.charAt(i + 1) == "b" &&
        testData.charAt(i + 2) == "r"
      ) {
        //   if <br> tag is receved skip 4 char and increment the i value
        capture = true;
        recursive = true;
      } else if (testData.charAt(i) == "<") {
        //   if < is found stop capturing
        capture = false;
      } else if (testData.charAt(i) == ">") {
        //   if > is found start  capturing but skip this iteration
        capture = true;
      }

      //   main capturing code
      if (capture) {
        if (testData.charAt(i) != ">") {
          finalText = finalText + testData.charAt(i);
        }
        if (recursive) {
          finalText =
            finalText +
            testData.charAt(i + 1) +
            testData.charAt(i + 2) +
            testData.charAt(i + 3);
          i = i + 3;
        }
      }
    }
    console.log(finalText);
  </script>
</html>

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