Update breadcrumbs dynamically by clicking on each horizontal panel

I've been dealing with a problem for the past 24 hours. I want to implement a horizontal accordion with breadcrumbs on a webpage. How can I achieve this dynamically, so that when a user clicks on any link in the accordion, the breadcrumbs update simultaneously? It's proving to be quite tricky for me. Any suggestions?

Is there a way to update the breadcrumbs with every panel click?

$('.items a').on('click', function() {
                var $this = $(this),
                    $bc = $('<div class="item"></div>');

                $this.parents('li').each(function(n, li) {
                    var $a = $(li).children('a').clone();
                    $bc.prepend(' / ', $a);
                });
                $('.breadcrumb').html( $bc.prepend('<a href="#Title One">test</a>') );
                return false;
            }) 
article.accordion {
                display: block;
                width: 43em;
                margin: 0 auto;
                background-color: #666;
                overflow: auto;
                border-radius: 5px;
                box-shadow: 0 3px 3px rgba(0,0,0,0.3);
            }
            /* CSS styles continue here */
            article.accordion section,
            article.accordion section h2 {
                -webkit-transition: all 1s ease;
                -moz-transition: all 1s ease;
                -ms-transition: all 1s ease;
                -o-transition: all 1s ease;
                transition: all 1s ease;
            }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

            <!DOCTYPE html>
            <html xmins="http://www.w3.org/1999/xhtml">
            <head>
            <title></title>
            </head>
              
            <body>
              
              <div class="breadcrumb">
                <div class="item"><a href="#acc1">Test/ </a></div>
            </div>
             <br>
            <article class="accordion">
                <section id="acc1">    
                   <h2><a href="#acc1"  class="items" >Title One</a></h2>
                    <p>This content appears on page 1.</p>       
                </section>
                <section id="acc2">
                   <h2><a href="#acc2"  class="items">Title Two</a></h2>
                    <p>This content appears on page 2.</p>
                     </div>
                </section>
                <section id="acc3">
                    <h2><a href="#acc3"  class="items" >Title Three</a></h2>
                    <p>This content appears on page 3.</p>
                </section>
                <section id="acc4">
                    <h2><a href="#acc4">Title Four</a></h2>
                    <p>This content appears on page 4.</p>
                </section>
                <section id="acc5">
                    <h2><a href="#acc5">Title Five</a></h2>
                    <p>This content appears on page 5.</p>
                </section>
            </article>
            </body>
            </html>

Answer №1

You are attempting to execute the function on a elements within the .items class, but your example does not define any classes.

 $(document).ready(function () {
        $('.items').on('click', function () {

            var $this = $(this),
                $bc = $('<div class="item"></div>');
            console.log($this);
            $this.parents('li').each(function (n, li) {
                var $a = $(li).children('a').clone();
                $bc.prepend(' / ', $a);
            });
            $('.breadcrumb').html($bc.prepend('<a href="#Title One">test</a>'));
            // return false;
        })
    });

Make sure to add the class attribute to your a element in the HTML code.

 <h2><a href="#acc1" class="items">Title One</a></h2>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is the canvas refusing to disappear?

My HTML5 Canvas element is not disappearing as expected. I wrote a timer function to hide the element when the timer hits 0. I tested this using an alert flag and it worked perfectly. Problem: if (TotalSeconds <= 0) { ctx.clearRect(0, 0, canvas.w ...

Utilize moment.js to format a datetime and display the corresponding timezone

I'm having trouble displaying timezones correctly using moment.js. I attempted to use the following code: var result = moment(someDate).format("MM/DD/YYYY HH:mm A Z"); This returns something like: 08/05/2015 06:18 PM +02:00, which is okay, but I w ...

Is there a javascript function that performs the reverse action of indexof()?

Is there a JavaScript function that is the opposite of indexOf()? In my code, when you press the button it will display 3 [from indexOf(".")]. However, I am looking for a function that is the opposite of indexOf(), which would show me 2 [decimal]. http: ...

Failed Ajax call! Utilizing Jquery to send a basic request with the .ajax function

Below is the jquery code snippet used to invoke the ajax function: function sendAjax(type, succ){ //where succ denotes the success callback if (type=="") { $("#txtResp").empty(); return; } if(succ === undefined) { succ ...

Updating Website Content Using JavaScript Library or Asynchronous JavaScript and XML

Can jQuery be used to alter plain HTML text? For instance, suppose I have a time displayed as 18:00 in simple HTML text - is it feasible to implement a drop-down menu featuring options like +1, +2, +3 and so on, or -1, -2, -3 and so forth? This way, selec ...

How can I retrieve objects using the JSON function?

I need to design a function that returns objects like the following: function customFunction(){ new somewhere.api({ var fullAddress = ''; (process address using API) (return JSON data) })open(); } <input type= ...

The Bootstrap image fails to adhere to the size of its parent flex container

I am having trouble positioning an image, heading, and a button on top of another image. The issue arises when viewing the content on small screens as the smaller image fails to resize properly and extends beyond the background of the larger holding imag ...

Adjusting the settimeout delay time during its execution

Is there a way to adjust the setTimeout delay time while it is already running? I tried using debounceTime() as an alternative, but I would like to modify the existing delay time instead of creating a new one. In the code snippet provided, the delay is se ...

Error: Query has already been processed: Updating Todo with ID "612df063a8f"

After updating mongoose to the latest version (6.0.2), I encountered an error that crashes the application whenever .updateOne() is executed. However, the object is still updated inside the database. Below is my code snippet: async(req,res) => { a ...

The function .save() in Mongoose is limited to executing only five times

While working with my House data seeder, I encountered a strange issue. Even though my code loops through the end, it only saves 5 records in the month model. const insertMonths = houses.map((house, i) => { const months = new Month({ year: &qu ...

A guide to efficiently fetch JSON data synchronously using AngularJS

Trying to fetch data from a JSON file using AngularJS results in an error due to the asynchronous nature of the call. The code moves on to the next step before receiving the data, causing issues. The $http.get method was used. $http.get('job.json&apo ...

Using infoWindows with multiple markers in Google Maps

i have developed a custom Google Maps application that pulls data from a CSV file. The functionality works well, but I am facing an issue with the infoWindow when looping through all the objects. It seems like the problem stems from the marker variable bei ...

What could be the reason behind ng-bind-html only displaying text and not the link?

Utilizing ng-repeat to exhibit a list on my webpage. One of the fields in my data contains a URL that I want to display as an actual link within my HTML page. Please refer to the screenshots below: My HTML: https://i.sstatic.net/2Gj1U.png My rendered pa ...

The synchronization between Typescript and the HTML view breaks down

I am currently working on an application that retrieves user event posts from MongoDB and displays them in HTML. In the Event-post.ts file, inside the ngOnInit() function, I have written code to retrieve the posts using the postsService.getPosts() method. ...

Executing vitest on compiled javascript files

Currently facing issues running vitest on compiled JavaScript. Numerous errors are appearing, such as: TypeError: Cannot read properties of undefined (reading 'spyOn') TypeError: Cannot read properties of undefined (reading 'mock') and ...

jQuery conceal input field after it has been displayed

I've created an HTML form with two different sections for search purposes. The first section displays basic fields, and by clicking on "Advanced Search" (in my case, "Расширенный поиск"), additional fields are revealed. Everything work ...

Assist me in temporarily altering the color of menu items in a sequential manner upon the page's loading

Currently, I'm working on a website that features a subtle dark grey menu at the top of every page. The menu is built using HTML and CSS with a list structure. To highlight the corresponding menu item based on the current page, I am utilizing the ID a ...

Set a variable to contain a specific scope in JavaScript

I'm struggling to pass user input from a text box stored in $scope to a firebase query. Here's the code I have so far: $scope.array = []; $scope.addListItem = function(quote){ $scope.array.unshift(quote); console.log(quote) this.customQuote = ...

PHP incorporate diverse files from various subfolders located within the main directory

My question is similar to PHP include file strategy needed. I have the following folder structure: /root/pages.php /root/articles/pages.php /root/includes/include_files.php /root/img/images.jpg All pages in the "/root" and "/root/art ...

Having trouble with parsing JSON data using jQuery?

I am currently working on retrieving a result set using postcodes with jQuery autocomplete. However, the code I have implemented seems to be displaying an empty set of rows. <html lang="en"> <head> <meta charset="utf-8> <title>Ausp ...