Optimise your jQueryAdd to My Brief

Benjamin Tinker | 10/06/2010

There are many jQuery plugins out there that allow for dropdown menus to work. There are too many to choose from and here are 38 as an example.

During development I like to set up my CSS and pages prior to having to include 3rd party software to make functionality work. This ensures that my layouts are correct and I can produce some clean code that does what I require and only what I require. Yet when including a jQuery plug-in they always come with lots and lots of their own CSS which can be a nightmare at best to retro-fit to your standards. So, why not just dump the 3rd party plug-in and develop a function that does all you want like this one;

function setTopNav() {
var tabs = $('.topnav ul.topnavroot > li');

//find those without nosub
var subtabs = tabs.not('.nosub');

//bind hover event
subtabs.each(function () {
$(this).hover(
function () { $(this).find('.dropdown').show(); },
function () { $(this).find('.dropdown').hide(); }
);
});
}

The benefit of this is that it only attaches dropdown actions to those navigation items that actually have a dropdown menu. My mark-up tags each navigation item with a ‘.nosub’ for ease of finding them. I know I could have done another query to find those without sub menus but for ease I just reduced it to one line. The rest just binds my hover action to those navigation items i want the dropdown to work on. Simple, concise and it works in all browsers.

tags: Web Development

Interested in learning more?

Wiliam is a leading supplier of web solutions and can provide expert advice to assist your business or organisation online.

Make enquiry   Request quotation

Comments

Share this page

Print this page Email this page to a friend Bookmark and Share

No Brief added yet.

My Brief results-driven approach emphasises a commitment to our clients needs through strategic analysis and best-practice.

Add to My Brief

Click on this icon to save item into My Brief. Retrieve, email and print

Drag & Drop

Drag & drop any iten with this icon into My Brief. Retrieve, email and print

Search Blogs