jquery - Click on LI, show/hide UL -
i trying toggle nav, can't seem figure out whats wrong code. it's pretty messy using cms has created id , class used in list menu. ideally have better name list.
i have posted code on https://jsfiddle.net/chachacallis/amfmsors/1/
$(document).ready(function () { $('ul ul').hide(); $('ul li span.section_title a').removeattr("href"); $('ul li span.section_title > a').click(function (event) { $('ul ul').hide('slow'); $(this).parent().find('ul').toggle('slow'); }); });
updated if didn't want default action disable home , other items not have sub menu? how add existing code?
firstly, jsfiddle did not include jquery did nothing @ all. secondly, removing href
attribute a
elements renders them un-clickable. if want stop link action being followed on click need use preventdefault()
on raised event. finally, dom traversal incorrect. need use closest('li').find()
, parent()
of a
span
not contain ul
. putting in action, this:
$(document).ready(function () { $('ul ul').hide(); $('ul li span.section_title > a').click(function (e) { e.preventdefault(); $('ul ul').hide('slow'); $(this).closest('li').find('ul').toggle('slow'); }); });
Comments
Post a Comment