Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <main>
  <h1>Auto tạo menu cho H2, H3</h1>
  <p>Nhấn để scroll đến Heading</p>
  <div class="js-table-of-content table-of-content"></div>

  <div class="page-content">
    <h2>Heading 1 - H2</h2>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <h2>Heading 2 - H2</h2>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <h2>Heading 3 - H2</h2>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <h3>Heading 4 - H3</h3>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <h2>Heading 5 - H2</h2>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
  </div>
</main>
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Roboto);

body {
  font-family: Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.57;
  background: #FFFF40;
  min-height: 100%;
}

main {
  background: #fff;
  max-width: 1024px;
  margin: auto;
  padding: 25px;
  border-radius: 5px;
}

.table-of-content {
  background: #ddd;
  padding: 15px;
  border-radius: 5px;
  .title-group {
    .title {
      font-size: 120%;
      font-weight: bold;
    }
  }

  .list {
    .heading-3 {
      padding-left: 15px;
    }
  }
}

              
            
!

JS

              
                (function ($) {});
setList();

function setList() {
  var tabId = 0;
  // $(".js-table-of-content").remove();

  var segments = [],
    arrayId = [],
    html =
      '<div class="title-group"><span class="title">MỤC LỤC</span></div><div class="list js-list">';

  $(".page-content h2, .page-content h3").each(function () {
    var data = getData(this, segments),
      hId = "tab" + tabId + "_h" + data.hLevel + segments.join("_");

    // add Level to array
    arrayId.push(data.hLevel);

    // Set data
    segments = data.segments;
    html +=
      '<p class="heading-' +
      data.hLevel +
      '"><a href="#' +
      hId +
      '">' +
      data.hText +
      "</a></p>";

    // Add ID to element
    $(this).attr("id", hId);
  });

  html += "</div>";

  console.log(html);

  // Append to HTML
  if (arrayId.length) {
    $(".js-table-of-content").append(html);
  }
}

// Get data heading
function getData(element, segments) {
  var hLevel = parseInt(element.nodeName.substring(1), 10),
    hIndex = parseInt(element.nodeName.substring(1)) - 1,
    hText = $(element).text();

  // Just found a levelUp event
  if (segments.length - 1 > hIndex) {
    segments = segments.slice(0, hIndex + 1);
  }

  // Just found a levelDown event
  if (segments[hIndex] == undefined) {
    segments[hIndex] = 0;
  }

  // count + 1 at current level
  segments[hIndex]++;

  return {
    hLevel: hLevel,
    hIndex: hIndex,
    hText: hText,
    segments: segments
  };
}

              
            
!
999px

Console