views:

226

answers:

1

2 part question here...I have several DIVs that use a simple .click show() function to display content. Is there a way i can combine these to trim my code? I wasnt sure, because they all are displaying DIV with Unique ID's.

$('a#hmo-blue-lnk1').click(function() {
    $('#hmo-blue1').show();
    return false;
  });

$('a#hmo-blue-lnk2').click(function() {
    $('#hmo-blue2').show();
    return false;
  });

$('a#hmo-blue-lnk3').click(function() {
    $('#hmo-blue3').show();
    return false;
  });

i close them via this snippet:

$('a.close').click(function() { 
     $('#hmo-blue1').hide();
     $('#hmo-blue2').hide();
     $('#hmo-blue3').hide();

     return false; 
  });

2nd part of the question... how can i integrate the aspect that the user can just hit the escape key to also hice (or close) the box that is shown?

+1  A: 

For the first part:

    for (i=1; i<=3; i++)
    {
           $('a#hmo-blue-lnk'+i).click(function() {
                 $('#hmo-blue'+i).show();
                 return false;
           });
    }

For the second part.

$(document).keypress(function(e) { 
    if (e.which == 27) {
          for (i=i; i <=3; i++)
          {
                 $('#hmo-blue'+i).hide();
          }
    }
});

You could change your hide function to a for loop too, then call it instead of closing too by doing $('a.close').click() which would call a click on a.close instead.

Gazler