0

I'm using the following JavaScript to toogle some information in my menu.

Is it possible to optimize it so I only have one function instead of n?

$(document).ready(function(){
 $("#km1").click(function(){
   $("#km1tog").slideToggle("slow");
 });
}); 

$(document).ready(function(){
 $("#km2").click(function(){
   $("#km2tog").slideToggle("slow");
 });
}); 

$(document).ready(function(){
 $("#km3").click(function(){
   $("#km3tog").slideToggle("slow");
 });
}); 
4
  • give them a class and use that instead of the id. $('.km').slideToggle('slow'); Commented Apr 11, 2014 at 9:45
  • @MarkWalters class will hide all but OP want only particular item to hide. Commented Apr 11, 2014 at 9:46
  • @ZaheerAhmed. Sorry I meant. $('.km').click(function() { $(this).slideToggle('slow'); }); Commented Apr 11, 2014 at 9:47
  • 1
    looks like belongs to codereview.stackexchange.com Commented Apr 11, 2014 at 9:48

5 Answers 5

3

You can use for loop and create an IIFE, to retain the value of i.

$(document).ready(function() {
    for (var i = 1; i <= 3; i += 1) {
        (function(i) {
            $("#km" + i).click(function() {
                $("#km" + i + "tog").slideToggle("slow");
            });
        }(i));
    }
});
Sign up to request clarification or add additional context in comments.

Comments

3

try:

$(document).ready(function(){
  $("#km1,#km2,#km3").click(function(){
    $("#"+this.id+"tog").slideToggle("slow");
  });
});

Comments

1

For a start you only need one $(document).ready call, so that will optimise it slightly. As for the rest, it depends greatly on the structure of the markup.

Ideally you wouldn't use ID's on the elements, you would simply reference them by their relationship, for example:

$(document).ready(function(){
    $(".top-menu").click(function(){
        $(this).find(".sub-menu").slideToggle("slow");
    });
}); 

Comments

0

Have you tried this?

$(document).ready(function() {
    $.each([1,2,3,4,5,6,7,8,9,10], function(i) {
        $("#km" + i).click(function(){
            $("#km" + i + "tog").slideToggle("slow");
        });
    });
});

3 Comments

$.each with a hardcoded array instead of a for loop? Wut?
Do you know whether it is hardcoded in the actual example?
The OP hardcoded 3 id's. That's a bad practice, and shouldn't be encouraged. Especially if the suggested alternative is to hardcode all iterations of a simple for loop. Your code is saying: for(var key in [1,2,3,4,5,6,7,8,9, 10]) instead of for(var i = 1; i <= 10; i++)
0

If possible add a class to all of your "clickable" divs and try something like this:

$(document).ready(function(){
    $(".km").click(function(){
        id = $(this).attr('id');
        $("#"+id+"tog").slideToggle("slow");
    });
}); 

1 Comment

$(this).attr('id');... You might not need jQuery: this.id

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.