tags:

views:

86

answers:

3

Hi all!

I am trying to create a jQuery hover function that will highlight a different section of the page depending on what number EQ (index position number) the div is.

What I want to do is say "when you hover over the #photoContent div, check what it's EQ number is. If it's the Xth div, then highlight the Yth p in the sidebar"

    $('#photoContent div').hover(function () {  
        if( $(this).filter(':lt(5)') ) {
            $('#photoSidebar').find('p:eq(0)').addClass('currentPhoto');
        }

        if( $(this).filter(':gt(5)') ) {
            $('#photoSidebar').find('p:eq(1)').addClass('currentPhoto');
        }
    }, function () {
        $('#photoSidebar').find('p').removeClass('currentPhoto');
    });

The above code obviously does not accomplish this, but the concept/functionality is what I'm going for. Thanks for your help!

+1  A: 

First, hoverIntent is not a built-in jQuery event. Do you mean hover(fnOver, fnOut) ?

Second, you could rewrite the if statements to explicitly test for a match:

if($(this).is(":lt(5)")) { 
    $('#photoSidebar p').eq(0).addClass('currentPhoto');
}

if($(this).is(":gt(5)")) {
    $('#photoSidebar p').eq(1).addClass('currentPhoto');
} 

OR

var idx = $(this).index();
if(idx > 5) { 
    $('#photoSidebar p').eq(0).addClass('currentPhoto');
}

if(idx < 5) {
    $('#photoSidebar p').eq(1).addClass('currentPhoto');
} 

Also, this is just my style, but why not use regular quotes " instead of single quotes?

Jeff Meatball Yang
Everyone has their own style. For me, it's `"` for HTML and `'` for code (it looks prettier in my opinion).
Casey Hope
Sorry, forgot to mention I'm using the hoverIntent plugin for jQuery... regardless, this isn't really an answer to the question...
j-man86
The above code throws an error: $(this).lt is not a function
j-man86
Whoops - I thought they were functions. I've edited my answer with a couple other things to try. You could also try debugging within the browser. IE8, FF, and Chrome all allow you to set breakpoints to pause the script on a certain line.
Jeff Meatball Yang
A: 

I'm not sure why your code doesn't work, other than the user of hoverIntent as Jeff pointed out, but it's a tad convoluted given that it's all in the same document, couldn't you go with:

$('#photoContent div').hover(function () {    
    if( $(this).filter(':lt(5)') ) {
        $('#photoSidebar p:eq(0)').addClass('currentPhoto');
    }

    if( $(this).filter(':gt(5)') ) {
        $('#photoSidebar p:eq(1)').addClass('currentPhoto');
    }

    $('#photoSidebar p').removeClass('currentPhoto');
});

I'm not sure why that last bit was a function either, since you probably want it to only fire when it's hovered.

I'm not sure the above will work, as I'm very unfamiliar with the filter() method, but it looks about right. does it get you closer?

Anthony
Hm... still nothing happens. I wish I could send you a link, but I'm developing it locally! The last part is the callback part of the hover function, it's what happens when you hover off of the element.
j-man86
A: 

I Believe I found what I was looking for, using the index() property:

    var divIndex = $(this).index();
        if (divIndex<=4) {
              //...
        }

This helped: http://stackoverflow.com/questions/1188760/in-jquery-how-do-you-find-out-the-eq-of-an-element-of-what-is-being-clicked

j-man86
nice find. I just posted the same thing.
Jeff Meatball Yang