html - jQuery - Hide primary ul once submenu is clicked -


i have jquery script outputs html menu json tree.

$(function () {      var data = {         menu: [{             name: 'croatia',             link: '0',             sub: null         }, {             name: 'england',             link: '1',             sub: [{                 name: 'arsenal',                 link: '0-0',                 sub: null             }, {                 name: 'liverpool',                 link: '0-1',                 sub: null             }, {                 name: 'manchester united',                 link: '0-2',                 sub: null             }]         }, {             name: 'spain',             link: '2',             sub: [{                 name: 'barcelona',                 link: '2-0',                 sub: null             }, {                 name: 'real madrid',                 link: '2-1',                 sub: null             }]         }, {             name: 'germany',             link: '3',             sub: [{                 name: 'bayern munich',                 link: '3-1',                 sub: null             }, {                 name: 'borrusia dortmund',                 link: '3-2',                 sub: null             }]         }]     };     var getmenuitem = function (itemdata) {         var item = $("<li>")             .append(         $("<a>", {             href: '#' + itemdata.link,             html: itemdata.name         }));         if (itemdata.sub) {             var sublist = $("<ul>");             $.each(itemdata.sub, function () {                 sublist.append(getmenuitem(this));             });             item.append(sublist);         }         return item;     };      var $menu = $("#menu");     $.each(data.menu, function () {         $menu.append(             getmenuitem(this)         );     });     $menu.menu(); }); 

and html:

<script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script> <ul id="menu"></ul> 

that job, outputs html menu, want submenu's hidden until visitor clicks on it.

so clear up, take menu example:

primary menu 1     - submenu item 1     - submenu item 1  primary menu 2     - submenu item 2     - submenu item 2 

once visitor clicks on "primary menu 1", should hide primary menu's , display submenu items.

how can archieve outcome?


update; managed working correctly (json loaded in different file now.)

$(window).load(function(){     $(function () {         var getmenuitem = function (itemdata) {             var item = $("<li>")                 .append(             $("<a>", {                 href: '#' + itemdata.link,                 html: itemdata.name             }));             if (itemdata.sub) {                 var sublist = $('<ul>', {'class':'sub-menu'});                 $.each(itemdata.sub, function () {                     sublist.append(getmenuitem(this));                 });                 item.append(sublist);             }             return item;         };          var $menu = $("#menu");         $.each(data.menu, function () {             $menu.append(                 getmenuitem(this)             );         });          $(".sub-menu").hide();          $("li").click(function (e) {             e.stoppropagation();             $(this).children('ul').slidetoggle();         });     }); }); 

after menu creation code, want start adding class menu items have submenus. can in creation code.

var sublist = $('<ul>', {'class':'sub-menu'}); 

from there should able know menu items have submenus, can various things there.

one example give sub-menu default height of 0 in css. add click events <ul class="sub-menu"> parent <li> animate menu height, while adding class <li> know when it's open or closed.

$('.sub-menu').parent().on('click', function(){    $(this).toggleclass('active');    if ($(this).hasclass('active')){      $(this).children('.sub-menu').animate({'height':'auto');    } else {      $(this).children('.sub-menu').animate({'height':'0');    } }); 

you can take step further , check against menu items have submenus, make sure if opened 1 gets closed before new 1 opened.

//inside previous click handler 1st thing in it. $(this).siblings().removeclass('active'); 

not 100% if code work in situation (depending on existing styling) should provide idea you're trying achieve.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -