0

I am very new to jquery and am wondering how to combine jquery functions. Right now, my code is a bit of a mess! I've done some research and haven't been able to keep it functioning properly when I combine functions. I don't want to keep my code looking like this, but at least for now it is working properly (except in Firefox). What is the best practice for combining functions?

    <script type="text/javascript">
$( document ).ready(function() {
    jQuery(function(){
      jQuery("#music").click(function () {
        jQuery("#musicinfo").slideToggle('slow');
        jQuery("#fpinfo, #behindinfo, #behindinfo, #signupinfo").hide('slow');

      });
    });
    });
</script>





<script type="text/javascript">
$( document ).ready(function() {
    jQuery(function(){
      jQuery("#fproduct").click(function () {
        jQuery("#fpinfo").slideToggle('slow');
        jQuery("#musicinfo, #behindinfo, #behindinfo, #signupinfo").hide('slow');

      });
    });
    });
</script>




<script type="text/javascript">
$( document ).ready(function() {
    jQuery(function(){
      jQuery("#behind").click(function () {
        jQuery("#behindinfo").slideToggle('slow');
        jQuery("#fpinfo, #musicinfo, #signupinfo").hide('slow');

      });
    });
    });
</script>

  <script type="text/javascript">
  $( document ).ready(function() {
    jQuery(function(){
      jQuery(".exit").click(function () {
        jQuery("#behindinfo, #musicinfo, #fpinfo, #signupinfo").hide('slow');

      });
    });
    });
</script>

<script type="text/javascript">
$( document ).ready(function() {
    jQuery(function(){
      jQuery("#signup").click(function () {
        jQuery("#signupinfo").slideToggle('slow');
        jQuery("#fpinfo, #musicinfo, #behindinfo").hide('slow');

      });
    });
    });
</script>

  <script type="text/javascript">
  $( document ).ready(function() {
    jQuery(function(){
      jQuery(".exit").click(function () {
        jQuery("#behindinfo, #musicinfo, #fpinfo, #signupinfo").hide('slow');

      });
    });
    });
</script>



<script>
     $('#behind, #fproduct, #music, #signup').click(function(){
         var divLoc = $('#top').offset();
         $('html, body').animate({scrollTop: divLoc.top}, "slow");
     });
</script>
2
  • 2
    This is a better fit for codereview.stackexchange.com I think. Commented Feb 18, 2014 at 15:30
  • it doesn't sound like you want to combine functions. looks like you're trying to combine script blocks. simple answer is that you can just do it by copying all the code into one block ready function. as for how the code should be laid out, codereview is indeed a good place Commented Feb 18, 2014 at 15:40

3 Answers 3

1

Have one single JS block, in this block one single invocation of ready (which is just the $ function):

  <script type="text/javascript">
     $(function() {

         $("#whatever").click(function() {
           // stuff
         });

         $("#somethingelse").click(function() {
           // stuff
         });
     });
  </script>

Also, try working with classes and html layout instead of explicit IDs. For example, if all clickable items are buttons, and info divs are right after them, you can simplify this:

 $("#foobar").click(function() {
     $("#foobarinfo").slideToggle("slow");
 });
 $("#blah").click(function() {
     $("#blahinfo").slideToggle("slow");
 });
 //etc, many many times

to something like:

  $("button").click(function() {
     $(this).next().slideToggle("slow");
  });
  // that's all

I guess your entire piece of code can be rewritten in three or four lines. Feel free to share your html markup if you're interested.

Sign up to request clarification or add additional context in comments.

Comments

0

Something like that :

    <script type="text/javascript">
$( document ).ready(function() {

      jQuery("#music").click(function () {
        jQuery("#musicinfo").slideToggle('slow');
        jQuery("#fpinfo, #behindinfo, #behindinfo, #signupinfo").hide('slow');

      });


      jQuery("#fproduct").click(function () {
        jQuery("#fpinfo").slideToggle('slow');
        jQuery("#musicinfo, #behindinfo, #behindinfo, #signupinfo").hide('slow');

      });

        jQuery("#behind").click(function () {
        jQuery("#behindinfo").slideToggle('slow');
        jQuery("#fpinfo, #musicinfo, #signupinfo").hide('slow');

      });

       jQuery(".exit").click(function () {
        jQuery("#behindinfo, #musicinfo, #fpinfo, #signupinfo").hide('slow');

      });

      jQuery("#signup").click(function () {
        jQuery("#signupinfo").slideToggle('slow');
        jQuery("#fpinfo, #musicinfo, #behindinfo").hide('slow');

      });

      jQuery(".exit").click(function () {
        jQuery("#behindinfo, #musicinfo, #fpinfo, #signupinfo").hide('slow');

      });

    $('#behind, #fproduct, #music, #signup').click(function(){
         var divLoc = $('#top').offset();
         $('html, body').animate({scrollTop: divLoc.top}, "slow");
     });
    });


</script>

1 Comment

You're welcome. You can also replace "JQuery" to "$".
0

After combining and consistently using $ sign for jquery we get something like this:

<script type="text/javascript">
$( document ).ready(function() {

    $("#music").click(function () {
            $("#musicinfo").slideToggle('slow');
            $("#fpinfo, #behindinfo, #behindinfo, #signupinfo").hide('slow');
            });
    $("#behind").click(function () {
        $("#behindinfo").slideToggle('slow');
        $("#fpinfo, #musicinfo, #signupinfo").hide('slow');
            });
    $("#fproduct").click(function () {
        $("#fpinfo").slideToggle('slow');
        $("#musicinfo, #behindinfo, #behindinfo, #signupinfo").hide('slow');
            });
    $(".exit").click(function () {
        $("#behindinfo, #musicinfo, #fpinfo, #signupinfo").hide('slow');
            });
    $("#signup").click(function () {
        $("#signupinfo").slideToggle('slow');
        $("#fpinfo, #musicinfo, #behindinfo").hide('slow');

            });
      $(".exit").click(function () {
        $("#behindinfo, #musicinfo, #fpinfo, #signupinfo").hide('slow');

      });

     $('#behind, #fproduct, #music, #signup').click(function(){
         var divLoc = $('#top').offset();
         $('html, body').animate({scrollTop: divLoc.top}, "slow");
     });
});
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.