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

106
Vistas
Generate Table of content dynamicaly with jquery

How do I generate table of contents dynamically. following is my code but its only for one heading tag h3. I need it to work for all headings. Here is my following sample format of the post :

<html>
<head></head>
<body>


<div id="tableofcontent"></div>
<div class="entry-content">
<h1 id="Test1">Main Heading</h1>
<p>Lorem Ipsum Lorem IpsumLorem IpsumLorem Ipsum</p>
<h2 id="Test2"> Sub Heading</h2>
<p>Lorem Ipsum Lorem IpsumLorem IpsumLorem Ipsum</p>
  <h3 id ="Test3">Sub Sub Heading</h3>
    <p>Lorem Ipsum Lorem IpsumLorem IpsumLorem Ipsum</p>
    <h4>Sub Sub Heading</h4>
    <p>Lorem Ipsum Lorem IpsumLorem IpsumLorem Ipsum</p>
   </div>
   </body> 
</html>

How do I generate table of contents dynamically. following is my code but its only for one heading tag h3. I need it to work for all headings.

jQuery(document).ready(function($) {
  var $aWithId = $('.entry-content h3[id]');
  if ($aWithId.length != 0) {
    if ($aWithId.length > 0) {
      $('#tableofcontent').prepend('<nav class="toc"><h3 class="widget-title">Table of Contents</h3><ol></ol></nav>');
    }

  }
   

var $aWithId = $('.entry-content h3[id]');
if ($aWithId.length != 0) {

  $('.entry-content').find($aWithId).each(function() {
    var $item = $(this);
    var $id = $(this).attr('id');
    var li = $('<li/>');
    var a = $('<a/>', {
      text: $item.text(),
      href: '#' + $id,
      title: $item.text()
    });
    a.appendTo(li);
    $('#tableofcontent .toc ol').append(li);
  });
}
});

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