views:

91

answers:

2

Hey guys!

I have 5 diffrent scripts on page, which all do the same:

<script language="javascript">
        $(document).ready(function(){
            $(".hideshow-news").hide();
   $(".roll-li-news").click(function(){
    if ($(".hideshow-news").is(":hidden")) {
                    $(".hideshow-news").slideDown("slow");
     $(".roll-li-news").removeClass('inactive');
                }
                else {
                    $(".hideshow-news").slideUp("slow");
     $(".roll-li-news").addClass('inactive');
                }
   });
        });
    </script>
 <script language="javascript">
        $(document).ready(function(){
            $(".hideshow-users").hide();
   $(".roll-li-users").click(function(){
    if ($(".hideshow-users").is(":hidden")) {
                    $(".hideshow-users").slideDown("slow");
     $(".roll-li-users").removeClass('inactive');
                }
                else {
                    $(".hideshow-users").slideUp("slow");
     $(".roll-li-users").addClass('inactive');
                }
   });
        });
    </script>
 <script language="javascript">
        $(document).ready(function(){
            $('.hideshow-pages').hide();
   $('.roll-li-pages').click(function(){
    if ($(".hideshow-pages").is(":hidden")) {
     $(".hideshow-pages").slideDown("slow");
     $(".roll-li-pages").removeClass('inactive');
    }
    else {
     $(".hideshow-pages").slideUp("slow");
     $(".roll-li-pages").addClass('inactive');
     }
   });
        });
    </script>

Is there a way to make those 5 scripts global and join them in one script? I made different classes as my menu have been opening on many spots.

Thanks for help.

+1  A: 

Yes, delete the last four script tags and use this selector in the first one:

".hideshow-news, .hideshow-users, .hideshow-pages"

Cheers,

Boldewyn
its not as simple as just changing the root selector, the click event will need a bit of reworking too, but this is basicly a good start yes
Sander
Replace .click() with .toggle()
Soviut
A: 

A quick resolution to your problem follows. Delete all script tags and contents and just add this into your document preferably into the <head></head> section.

<script language="javascript">
    $(document).ready(function(){

      var arr = {"news", "pages", "users"};
      var x;
      for (x in arr){
        $(".hideshow-"+x).hide();

        $(".roll-li-"+x).click(function(){
          if ($(".hideshow-"+x).is(":hidden")) {
            $(".hideshow-"+x).slideDown("slow");
            $(".roll-li-"+x).removeClass('inactive');
          }else {
            $(".hideshow-"+x).slideUp("slow");
            $(".roll-li-"+x).addClass('inactive');
          }
        });
      }
    });
</script>

This should do what you need, i didn't test it though. If you have other places that you use this with same structure, change the array like this:

var arr = {"news", "pages", "users", "members", "others"};

but as Boldewyn says you should go over basics more.

Hope it helps, Sinan.

Sinan Y.
Thanks!Great solution, and yes like you said, I should sit down and take one good deep look at jQuery!Thanks again.
AnzeT