Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery - dynamic breadcrumb

I'm trying to create a breadcrumb system for my site using the following:

<div class="breadcrumb">
    <div class="item"><a href="#home">Home / </a></div>
</div>

<div class="items">
   <ul>
     <li><a href="#test1">Test 1</a></li>
     <li><a href="#test1">Test 2</a>
        <ul>
            <li><a href="">Level 1</a></li>
            <li><a href="">Level 2</a></li>
        </ul>
     </li>
     <li><a href="#test1">Test 3</a></li>
  </ul>
</div>

What I'm looking to do is so when a user clicks on Test 1 the breadcrumb is Home / Test 1, if they then click on Test 2 and then Level 1, the breadcrumb will become Home / Test 1 / Level 2

I've been looking at jQuery to do this, but not 100% sure how best to approach it.

Any ideas would be much appreciated

Thanks

like image 683
user1970557 Avatar asked Mar 24 '23 01:03

user1970557


1 Answers

example http://jsfiddle.net/mPsez/3/

$('.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="#home">Home</a>') );
    return false;
}) 
like image 193
David Fregoli Avatar answered Apr 01 '23 06:04

David Fregoli