-2

Is it possible to compress this code below into few lines? There are similar _jq_overlay_x and _jq_overlay_y statements for 8 different events. I think that will be better to compress those lines which will be better for optimization.

Thanks in advance

  $(document).ready(function(){
        $('#_jq_overlay_x').click( function(event) { 
            event.preventDefault(); 
            $('#_jq_overlay1').fadeOut(500); 
        });

        $('#_jq_overlay_y').click( function(event) { 
            event.preventDefault(); 
            $('#_jq_overlay1').fadeIn(500); 
        });
        $('#_jq_overlay_x').click( function(event) { 
            event.preventDefault(); 
            $('#_jq_overlay2').fadeOut(500); 
        });

        $('#_jq_overlay_y').click( function(event) { 
            event.preventDefault(); 
            $('#_jq_overlay2').fadeIn(500); 
        });
            $('#_jq_overlay_x').click( function(event) { 
            event.preventDefault(); 
            $('#_jq_overlay3').fadeOut(500); 
        });

        $('#_jq_overlay_y').click( function(event) { 
            event.preventDefault(); 
            $('#_jq_overlay3').fadeIn(500); 
        });
            $('#_jq_overlay_x').click( function(event) { 
            event.preventDefault(); 
            $('#_jq_overlay4').fadeOut(500); 
        });

        $('#_jq_overlay_y').click( function(event) { 
            event.preventDefault(); 
            $('#_jq_overlay4').fadeIn(500); 
        });
            $('#_jq_overlay_x').click( function(event) { 
            event.preventDefault(); 
            $('#_jq_overlay5').fadeOut(500); 
        });

        $('#_jq_overlay_y').click( function(event) { 
            event.preventDefault(); 
            $('#_jq_overlay5').fadeIn(500); 
        });
            $('#_jq_overlay_x').click( function(event) { 
            event.preventDefault(); 
            $('#_jq_overlay6').fadeOut(500); 
        });

        $('#_jq_overlay_y').click( function(event) { 
            event.preventDefault(); 
            $('#_jq_overlay6').fadeIn(500); 
        });
            $('#_jq_overlay_x').click( function(event) { 
            event.preventDefault(); 
            $('#_jq_overlay7').fadeOut(500); 
        });

        $('#_jq_overlay_y').click( function(event) { 
            event.preventDefault(); 
            $('#_jq_overlay7').fadeIn(500); 
        });
            $('#_jq_overlay_x').click( function(event) { 
            event.preventDefault(); 
            $('#_jq_overlay8').fadeOut(500); 
        });

        $('#_jq_overlay_y').click( function(event) { 
            event.preventDefault(); 
            $('#_jq_overlay8').fadeIn(500); 
        });
 });

3 Answers 3

1

Either use classes instead of ids or combine all of the similar ones into something like:

$("#object1, #object2, #object3, ...").click(function() { ... });

Then, just use one click and check if the object .is(':visible') to hide/show it.

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

1 Comment

This really doesn't sound like it answers the question at all.
1

I think it can be shortened to:

$('#_jq_overlay_x,#_jq_overlay_y').click( function(event) { 
   event.preventDefault();
   for (var i=1;i<9;(i=i+1)){
     $('#_jq_overlay'+i)[(/_y/i.test(this.id) ? 'fadeIn' : 'fadeOut')](500);
   }
});

See this jsfiddle mockup

4 Comments

this works in Firefox, Chrome, Safari, Opera, but it seems doesn`t work in IE7/8
The click handler function works. In the jsfiddle mockup, the boxes are not shown in IE7/8, because IE7/8 don't know the css rgb(a) property. I've adjusted the example, now it also works in all IE versions.
+1 for a REALLY short solution and some cool scripting, but looking at the question I doubt the OP has any idea what is actually going on in that fiddle ?
thanks @adeneo. I've added a few comments to the jsfiddle example
0
$(document).ready(function(){
    var elms=[];
    for (var i=1; i<=8;i++) {
        elms.push($('#_jq_overlay'+i)[0]);
    }
    $('#_jq_overlay_x').click( function(event) { 
        event.preventDefault(); 
        $(elms).fadeOut(500); 
    });

    $('#_jq_overlay_y').click( function(event) { 
        event.preventDefault(); 
        $(elms).fadeIn(500); 
    });
});​

Here's a FIDDLE to show it working!

6 Comments

Nope, this brokes whole preview
$('#_jq_overlay'+i)[0] -> $('#_jq_overlay'+i).get(0) ? Just a thought
@Marko - It seems to work fine for me, added a Fiddle to my answer so you can see it affecting all the elements.
@Johan - Those two do the exact same thing!
@adeneo I know, I just prefer the other one :-)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.