1

With this code, when you hover over anything with the id="trigger*", it shows everything with the id="panel*" I would like it, for trigger1 to show panel1, for trigger2 to show panel2.

Is that possible? This is my code:

$(document).ready(function(){
hovered = false;

$('*[id^=trigger]').bind('mouseenter mouseleave', function(event) {
switch(event.type) {
    case 'mouseenter':
       // when user enters the div

       $('*[id^=panel]').show('fast');

    break;
    case 'mouseleave':
      // leaves
         setTimeout(function(){
  if(!hovered) {
      $('*[id^=panel]').hide('fast');
      }}, 250);
    break;
}
});

$('*[id^=panel]').mouseover(function(){
hovered = true;
}).mouseout(function(){
hovered = false;
$('*[id^=trigger]').trigger("mouseout");
});

});
Chezzo
  • 11
  • 1
  • 3

3 Answers3

0

If I'm understanding your requirements, you would like to show the closest panel to the trigger that was triggered. Use .closest() for this.

Change this:

$('*[id^=panel]').show('fast');

To this:

$(this).closest('*[id^=panel]').show('fast');

http://api.jquery.com/closest/

James Hill
  • 60,353
  • 20
  • 145
  • 161
0

use a regular expression, or javascript split function to split the id into two parts and that way obtain the id

like for example in my code i use

var draggable_id = $(this).attr('id').split('button')[0];
max4ever
  • 11,909
  • 13
  • 77
  • 115
0

extract the id from the trigger object and use that id to find the panel object.

$('*[id^=trigger]').bind('mouseenter mouseleave', function(event) {
  // extract id from trigger object
  var id = this.id.substring(7);
  switch(event.type) {
  case 'mouseenter':
    // when user enters the div
    $('*[id^=panel'+id+']').show();
  break;
  case 'mouseleave':
    $('*[id^=panel'+id+']').hide();
  break;
  }
});
ntangjee
  • 301
  • 1
  • 2