javascript - What is the best way to user jQuery selector at this problem? -


i don't know best title this. basically, on page have dropdown menu implemented jquery (.slide). dropdown appear in both @ top , bottom of page, it's easier user scroll down , can still use dropdown menu. page display use rail partial can refactor quite easy.

the problem is, since 2 dropdown menus in same page, can't not have same id have same functionality, when user click opens , show other options. best way let them use same logic different id , less code possible.

i don't want this.

        $('.sub_export_record1').hide();          $('.export_record_link1').click( function(e) {             e.preventdefault();         });          $('.export_record1').click( function(e) {              if ( $(".sub_export_record1").is(":hidden") )             {                 $('.sub_export_record1').slidedown("slow");             }             else             {                 $('.sub_export_record1').hide();             }         }); 

and second one

        $('.sub_export_record2').hide();          $('.export_record_link2').click( function(e) {             e.preventdefault();         });          $('.export_record2').click( function(e) {              if ( $(".sub_export_record2").is(":hidden") )             {                 $('.sub_export_record2').slidedown("slow");             }             else             {                 $('.sub_export_record2').hide();             }         }); 

thanks lot. :)

html

<ul> <li class="export_record">     <%= link_to "export record"%>     <ul class="sub_export_record">         <li><%= link_to "export photo wall"%></li>         <li><%= link_to "export pdf"%></li>         <li><%= link_to "export csv"%></li>     </ul> </li> </ul> 

there no need identify elements. give them same class (as have in snippet):

$('.sub_export_record').hide();  $('.export_record').click( function(event) {     event.preventdefault();      var $sub = $(this).children(".sub_export_record");            if ( $sub.is(":hidden") ) {         $sub.slidedown("slow");     }     else {         $sub.hide();     } }); 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -