views:

33

answers:

3

So I have content boxes that close and expand when you click an arrow. The content box has two classes for telling whether it is open or closed (.box_open, .box_closed). A hover function is assigned to box_open so when it is open and you hover over the header, the arrow appears. However, I don't want this to happen when the box is closed, as I want to arrow to remain visible when the box is closed. When the box closes, the box_open class is removed, but the function assigned to that class still works.

Here's the jquery code for the two functions. You can also see them in the head of the demo below.

    // Display Arrow on Box Header Hover

        $(".box_open").hover(
            function () {
                $(this).find('a').show();
            }, 
            function () {
                $(this).find('a').hide();
            }
        );

    // Open and Close Boxes:

        $(".box_header a").click(
            function () {
                $(this).parent().next('.box_border').stop().toggle();
                $(this).parent().toggleClass("box_open");
                $(this).parent().toggleClass("box_closed");
                return false;
            }
        );

Can anyone take a look at what the problem is?

Here's the demo url: Demo Url

A: 

Put this in your css file.

.box_closed a {
    display: inline !important;
}
Ivo Sabev
That makes no difference since the class .box_hover inst even in use.
Qwibble
@Qwibble I am sorry I meant .box_closed :)
Ivo Sabev
+2  A: 

The event handler is assigned to the element, not to the class label. So once assigned, removing the class name doesn't make the handler go away.

You could assign the event to 'box_header' and check for the existence of the 'box_open' class in your hover() code.

$('.box_header').hover(
function() {
    $(this).find('a').show();
},
function() {
    if($(this).hasClass('box_open'))
        $(this).find('a').hide();
});

EDIT: Added missing close parenthesis. Sloppy me!

patrick dw
I'm a bit confused on the syntax of the if statement. Aptana is flagging it up, but I can't for the life of me see what's wrong. I understand what the problem with my method is, and why this should work, but syntax is killing me lol
Qwibble
No worries, I've worked the syntax out =)
Qwibble
+1  A: 

Patrick's answer is correct, but there's a better way to do it: use jQuery.live(), which lets you "attach a handler to the event for all elements which match the current selector, now or in the future."

So, this should more-or-less get you what you want:

$('.box_open').live('hover', function (event) {
    if(event.type === 'mouseenter') {
        $(this).find('a').show();
    }
    else {
        $(this).find('a').hide();
    }
});

Poached pretty much straight from the jQuery docs.

Matt Ball
But you would need to unbind the event every time you close the box. This is much more work than a simple `if()` statement.
patrick dw
I had looked at the .live(), but it seemed to cause more problems than it fixed. The if statement works well and I understand the logic so I'll stick with it =)Cheers for the help man =)
Qwibble
@patrick: if I understand the question correctly, the event wouldn't have to be unbound when the box is closed because - I assume - closing the box means removing the `box_open` class and applying the `box_close` class. Am I wrong?
Matt Ball
+1 You are correct. I was thinking that `live()` behaved more like `bind()` with regard to modifying the selector on the element that received the event. Your method works. I would still tend to test for a flag (like the box_open class) since I think it is a less resource hungry method, and `live()` can present issues if you're going to rely on event bubbling.
patrick dw