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

219
Views
Eliminar el HTML interno de DIV hasta una etiqueta <p>

Digamos que tengo un div , como el siguiente:

 <div id="mainDIV">Area to remove <p>Area to not remove</p></div>

Quiero eliminar el contenido de div/innerHTML hasta la etiqueta p . Intenté eliminarlo de la siguiente manera:

 document.getElementById("mainDiv").innerHTML = ""

Si bien esto eliminó el área que quería eliminar, también eliminó el elemento p y su contenido.

¿Hay alguna forma de hacer esto? ¡Gracias!

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

0

Suponiendo que lo que se eliminará siempre será el primer nodo de texto , puede eliminar ese primer nodo secundario.

 const mainDiv = document.querySelector('#mainDIV'); mainDiv.removeChild(mainDiv.firstChild);
 <div id="mainDIV">Area to remove <p>Area to not remove</p></div>

Pero también cuida tu ortografía. mainDiv no es mainDIV .

about 4 years ago · Juan Pablo Isaza Report

0

Puede obtener el contenido de <p> y luego insertarlo en el HTML interno de #mainDIV, como:

 document.getElementById('mainDIV').innerHTML = document.getElementById('mainDIV').querySelector('p').outerHTML;
about 4 years ago · Juan Pablo Isaza Report

0

puede usar la función 'dividir' así como:

 var your_div = document.getElementById("mainDiv"); //splits the innerHTML of the div at "<p>" and selects the inside p side var just_the_p = your_div.innerHTML.split("<p>")[1]; your_div.innerHTML = "<p>"+just_the_p;

el HTML interno de tu div ahora es "Área para no eliminar" (más la etiqueta p)

también tenga en cuenta que la división eliminará el inicio de la etiqueta p, así que simplemente agréguela más tarde

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!