tags:

views:

40

answers:

1

i have a script where if you hover over a certin element, a unique DIV is displayed, and the others hide...very simple...

on page load, i call a div as the "intro" shown div....

what i want to accomplish, is if the other 5 elements are not being hovered over, it is displaying the intro div...

so, essentially in layman's terms:

show intro div if mouseover this div class, show this div ID, if not show intro div.

here is what i am using so far:

$(document).ready(function() {

$('.intro').show();

$('li.members, li.members-active').hover(function() {
    $('.members-show').show();
    $('.brokers-show').hide();
    $('.providers-show').hide();
    $('.employers-show').hide();
    $('.seniors-show').hide();
    $('.all').hide();
    return false;
  });



$('li.brokers, li.brokers-active').hover(function() {
    $('.brokers-show').show();
    $('.members-show').hide();
    $('.providers-show').hide();
    $('.employers-show').hide();
    $('.seniors-show').hide();
    $('.all').hide();
    return false;
  });

$('li.providers, li.providers-active').hover(function() {
    $('.providers-show').show();
    $('.brokers-show').hide();
    $('.members-show').hide();
    $('.employers-show').hide();
    $('.seniors-show').hide();
    $('.all').hide();
    return false;
  });

$('li.employers, li.employers-active').hover(function() {
    $('.employers-show').show();
    $('.brokers-show').hide();
    $('.providers-show').hide();
    $('.members-show').hide();
    $('.seniors-show').hide();
    $('.all').hide();
    return false;
  });

$('li.seniors, li.seniors-active').hover(function() {
    $('.seniors-show').show();
    $('.brokers-show').hide();
    $('.providers-show').hide();
    $('.employers-show').hide();
    $('.members-show').hide();
    $('.all').hide();
    return false;
  });

});
+1  A: 

You can simplify this significantly:

$(document).ready(function() {

    $('.intro').show();    // Show the intro by default
                           // Ideally, you'd skip this step and just make sure
                           // the intro was visible on load anyway

    $('li').hover(function() {   // Bind an event handler to every item you want to toggle            
        var associatedDivClass = $(this).attr('class').replace('-active', '-show');
        $('.' + associatedDivClass).show();
        $('.intro').hide();
    });
    $('li').mouseout(function() {
        var associatedDivClass = $(this).attr('class').replace('-active', '-show');
        $('.' + associatedDivClass ).hide();
        $('.intro').show();
    });

});

Restrict the 'li' selector as needed so you just target the items you want to toggle.

Dexter
Beat me to it.....+1
David Hoerster
How does that work? If each LI element is to display a unique DIV ID displaying unique content? I dont understand how setting LI to 'this' will show each unique ID? (sorry noob here)
tony noriega
@tony - fair point, i missed that part of your question. I've updated above to show how you can get the class name of the associated div using the current li's class name
Dexter
I will try this...in addition, if i have an anchor link inside the LI element: <li class="members"><a href="#" class="next">click to see more</a></li> how can i also make it so that not only if they mouse out of the LI element, but the a href as well, it will call .intro?
tony noriega
if the anchor is nested inside the li, then the above script will do what you're trying to do..?
Dexter