// This element is hidden by default so we need to delegate a click event that watches for elements // that have a class of "showSub" $(document).on('click', '.showSub li > ul > li > a', function(e) { e.preventDefault(); $(this).next().slideToggle(); });

It seems to work and allows for all elements to slide as required without having two different routines and works for the hidden elements. (I did add classes the the sub navs to identify them but could be done without the classes of course.).

chris_upjohn
—
2013-09-23T13:05:50Z —
#6

I simply took his code and re-worked it into a delegated event, I noticed it was overdone but it been so warm here in Melbourne I just got it done as quick as possible :), as for your code the only thing I would change is the preventDefault line so it comes before the slide toggle otherwise if that fails the default action will take place.

Also drop down menu's aren't typically coded like this any more using classes and what not, from the trend I've seen going around data attributes are becoming a thing when it comes to managing DOM elements, of course this shouldn't rule out classes all together but they are much easier to manage.

PaulOB
—
2013-09-23T13:25:26Z —
#7

chris_upjohn said:

as for your code the only thing I would change is the preventDefault line so it comes before the slide toggle otherwise if that fails the default action will take place.

Ah ok thanks Chris that's a good thing to remember:)

(Mind you if the slide failed then you may want the link to be actioned to allow navigation to a sub page as mentioned here..)

Also drop down menu's aren't typically coded like this any more using classes and what not, from the trend I've seen going around data attributes are becoming a thing when it comes to managing DOM elements, of course this shouldn't rule out classes all together but they are much easier to manage.

Yes, I've seen data attributes used a lot (especially in bootstrap) but of course only valid in html5 (although I gather they will do no harm elsewhere).

chris_upjohn
—
2013-09-23T23:11:39Z —
#8

Paul_O_B said:

(Mind you if the slide failed then you may want the link to be actioned to allow navigation to a sub page as mentioned here..)

I understand what that guy is getting at but not checking if for instance the variable my is not set is bad practice, allowing your code to fail on purpose is the wrong way to write an event as you have bound a custom action to it that should be carried out without any errors.

I understand what that guy is getting at but not checking if for instance the variable my is not set is bad practice, allowing your code to fail on purpose is the wrong way to write an event as you have bound a custom action to it that should be carried out without any errors.

Thanks for the explanation Chris

sessions
—
2013-09-26T03:28:56Z —
#10

chris_upjohn said:

I understand what that guy is getting at but not checking if for instance the variable my is not set is bad practice, allowing your code to fail on purpose is the wrong way to write an event as you have bound a custom action to it that should be carried out without any errors.