javascript - jquery slide menu - better way? -


i'm pretty new javascript/jquery , built simple slide menu.

it has 3 menus , each menu has submenu...everything working fine, want know if there's better way accomplish same task.

here's js code:

function menuopen(menu){     if(menu=='menu1'){         $("#sub2").slideup(400);         $("#sub3").slideup(400);         $("#sub1").slidetoggle(400);     }else if(menu=='menu2'){         $("#sub1").slideup(400);         $("#sub3").slideup(400);         $("#sub2").slidetoggle(400);     }else if(menu=='menu3'){         $("#sub1").slideup(400);         $("#sub2").slideup(400);         $("#sub3").slidetoggle(300);     } } 

without seeing html:

live demo

function menuopen(menu){           var num = menu.match( /\d+/ ); // regex expression retrieve number                      $('[id^=sub]').slideup();      // slide id starting sub     $('#sub'+num).slidetoggle();   // desired id :)   } 

use of jquery means want easily manipulate dom elements , means without seeing html sample of dom nodes , structure you're target it's hard make above even simpler.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -