Adding Menu separator Bars with jQuery.

Posted by Damodar Bashyal on April 30, 2012


I had a website whose menu items were loaded from different file blocks and cms static blocks. So, I decided to use jQuery for this purpose. So here is my menu.

<div class="quick-access">
    <div class="topLinks">
        <ul class="links">
            <li class="first">
                <a title="Goto Homepage." href="">Home</a>
                <a title="Find the store near you." href="">Store Locator</a>
                <a title="Contact us for anything you need help with." href="">Contact Us</a>
                <a title="Get the latest deals straight on to your emails." href="">Sign up for our latest Specials</a>
                <a title="Click here to goto My Account area." href="">My Account</a>
                <a title="Click here to Login and get members only specials." href="">Log In</a>

So, I used jQuery function to insert separators like this:

<script type="text/javascript">
    (function ($) {
        $('div.quick-access div.topLinks ul.links').children('li').each(function () {
            $('<li class="bar">|</li>').insertBefore($(this).next('li'));

That will add "<li class="bar">|</li>" after every LIs except first one.

and, this is my css:

.header .quick-access .links {
    color: #FFCC00;
    line-height: 34px;
    margin: 0;
    padding: 0 10px;

Update: Today I had to add separator again to another menu but it had no LIs but DIVs. That's not the issue exactly but menu items were separated with some other hidden div blocks which contained sub-menus. So, jQuery next() function didn't work. So tried with jQuery's :first selector and it worked as expected. So this is my new script.

    function () {
	    $('<div class="bar">|</div>').insertBefore($(this));

Stephen Musgrave posted on - Monday 8th of July 2013 06:35:48 AM

Thanks for this -- very helpful.
not published on website

QR Code: Adding Menu separator Bars with jQuery.