Open In App

How to create dynamic breadcrumbs using JavaScript?

Last Updated : 08 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to create dynamic breadcrumbs using JavaScript.

 A dynamic breadcrumb allows us to navigate to different pages within the navigational hierarchy, and thus we can organize various pages of the website in a hierarchical manner just like a folder-like structure.

Approach:

The following example is implemented using HTML and JavaScript.

HTML lists like <ol>,<ul>,<li> are used to create the navigation links. 

Breadcrumbs dynamic navigation is implemented using various JavaScript functions like jQuery prepend(),clone() and click() methods. On click of each navigation link <a> , the child nodes are appended to its parent along with the “GeeksforGeeks /” link in the bottom div with the class display. This whole navigation hierarchy is shown in the last HTML div using jQuery html() method.

Example: The following code demonstrates the above approach.

HTML




<!DOCTYPE HTML>
<html>
<head>
   
   <script src=
   </script>
    
</head>
<style>
   body{
     background-color:white;
   }
  .display{
    background-color:red; 
  }
    
</style>
<body>
  <h2 style="color:green;">GeeksforGeeks</h2>  
    
<div class="topics">
   <ol>
       <li><a href="#"><b>Searching</b></a>
         <ul>
          <li><a href="#">Linear Search</a></li>
          <li><a href="#">Binary Search</a></li>
         </ul>
       </li>
      <li><a href="#"><b>Sorting</b></a>
        <ul>
            <li><a href="#">Bubble Sort</a></li>
            <li><a href="#">Merge Sort</a>
                <ul>
                    <li><a href="#"><i>Recursive Merge Sort</i></a></li>
                    <li><a href="#"><i>Iterative Merge Sort</i></a></li>
                </ul>
            </li>
            <li><a href="#">Selection Sort</a></li>
            <li><a href="#">Insertion Sort</a
        </ul>
     </li>
     <li><a href="#"><b>Tree</b></a>
      <ul>
          <li><a href="#">Binary Tree</a></li>
          <li><a href=" #">Binary Search Tree</a></li>
        </ul>
     </li>
 
  </ol>
</div>
 
  <div class="display">
    <div class="syllabus">
      <a href="#">GeeksforGeeks / </a>
    </div>
  </div>
         
   
<script type="text/javascript">
 
   $('.topics a').on('click', function() {
    //selecting the syllabus class
      $select = $('<div class="syllabus"></div>');
      $(this).parents('li').each(function(n, li) {
         //Adding / to each anchor tag of li
          $select.prepend(' / ',$(li).children('a').clone());
      });
    // Displaying the hierarchical order of pages.
    $('.display').html(
      $select.prepend('<a href="#syllabus">GeeksforGeeks</a>'));   
})
 
</script>
</body>
   
</html>


Output:

  • Before execute:

  • After execute:



Similar Reads

React.js BluePrint Breadcrumbs Component Breadcrumbs Props
BlueprintJS is a React-based UI toolkit for the web. This library is very optimized and popular for building interfaces that are complex data-dense for desktop applications. Breadcrumbs Component provides a way for users to identify the path to the current resource in an application. There are different ways to design Breadcrumb. Breadcrumbs Props:
4 min read
How To Create Dynamic Breadcrumbs Component in NextJS?
Breadcrumbs are an essential navigational aid in web applications, especially in multi-level hierarchies. In a Next.js application, creating a dynamic breadcrumbs component can enhance the user experience by providing clear navigation paths. In this guide, we'll walk through the process of building a dynamic breadcrumbs component in Next.js. Prereq
3 min read
How to Create Breadcrumbs using HTML and CSS ?
Create an attractive Breadcrumbs navigation is quite difficult for NON-CSS experts. Without using CSS, the Breadcrumbs navigation will steal your website's gorgeousness. By using only HTML and CSS we can create an attractive Breadcrumbs navigation. In this article, the main focus will be CSS. We will create the structure for the breadcrumbs first a
4 min read
Bootstrap | Badges and Breadcrumbs
Badges: Badges are numbers associated with the link to indicate the number of items associated with the link. The notification number is seen when logged in to a particular website and tells the numbers of news or notifications to see by clicking it. Example: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;badge example&lt;/
4 min read
Materialize CSS Breadcrumbs
Breadcrumbs in materialize CSS is used when you have multiple layers of content to display your current location. Materialize CSS provides various CSS classes to create a nice breadcrumb in an easy way. It uses two classes i.e. nav-wrapper and breadcrumb. nav-wrapper is used to set the nav component as breadcrumb/nav bar wrapper. breadcrumb is used
1 min read
ReactJS Blueprint Breadcrumbs Component
BlueprintJS is a React-based UI toolkit for the web. This library is very optimized and popular for building interfaces that are complex data-dense for desktop applications. Breadcrumbs Component provides a way for users to identify the path to the current resource in an application. We can use the following approach in ReactJS to use the ReactJS B
3 min read
React MUI Breadcrumbs Navigation
MUI or Material-UI, it’s a React component library. It enables you to build your own design system and develop React applications faster. It is basically a design language that was developed by Google in 2014. It uses more Design and Animation, grid-system, and provides shadows and lightning effects. In React MUI Breadcrumbs Navigation, a breadcrum
3 min read
Spectre Breadcrumbs
Spectre Breadcrumbs offers us to design breadcrumbs. Breadcrumbs are used to indicate the current page’s location within a navigational hierarchy. To use breadcrumbs you need a container element with the breadcrumb class. Add child elements with the breadcrumb-item class. Spectre Breadcrumbs Class: breadcrumb: This class is used to create breadcrum
1 min read
Foundation CSS Breadcrumbs
Foundation CSS is an open-source &amp; responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to layout stunning responsive websites, apps, and emails that appear amazing &amp; can be accessible to any device. In this article, we will discuss the Breadcrumb component in Foundation CSS. Breadcrumb is a useful e
2 min read
Foundation CSS Kitchen Sink Breadcrumbs
Foundation CSS is an open-source and responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to layout stunning responsive websites, apps, and emails that appear amazing and can be accessible to any device. It is used by many companies such as Facebook, eBay, Mozilla, Adobe, and even Disney. The framework is bu
2 min read