0

I have a menu list, as I scroll over the menu list the related divs are shown or hidden. This works fine with the following:

$('.ov_menu li').mouseover(function(){
        var div_show = ($(this).parent().attr('href'));

        $('.homepage_display').hide();
        $(div_show).show();
        $('.ov_menu li').css('background-color','#ffffff')
        $(this).css('background-color','#cceffc');

        return false;
});

What is the most efficient way to display the relevant div when I click on the menu item, without having to type out the above code again (but using the click event rather than the mouseover event).

Markup:

<div class="ov_menu">
        <ul>
            <a href="#new"><li>Create New Check</li></a>
            <a href="#in_progress"><li>In Progress Checks</li></a>
            <a href="#completed"><li>Completed Checks</li></a>
            <a href="#archived"><li>Archived Checks</li></a>
        </ul>
</div>

<div class="homepage_display" id="new">
Content
</div>

<div class="homepage_display" id="in_progress">
Content
</div>

<div class="homepage_display" id="completed">
Content
</div>

<div class="homepage_display" id="archived">
Content
</div>
1
  • Would you mind showing your markup? Commented Apr 23, 2011 at 11:08

1 Answer 1

2

Just extract it out to use a named function.

function ShowSomething() {
    var div_show = ($(this).parent().attr('href'));

    $('.homepage_display').hide();
    $(div_show).show();
    $('.ov_menu li').css('background-color', '#ffffff')
    $(this).css('background-color', '#cceffc');

    return false;
}

$('.ov_menu li').mouseover(ShowSomething);
//or
$('.ov_menu li').click(ShowSomething);

Simple example on jsfiddle

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

2 Comments

Thanks Mark, sometimes the answer is so simple it stares me in the face.
No problem, glad I was able to help :)

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.