Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery select next div

Tags:

jquery

so code looks like this: How can I change div1 when hovering over current link1 ?

<ul>
    <li>
        <a class='link1'></a>
        <a class='link2'>
            <div class='div1'></div>
            <div class='div2'></div>
        </a>
    </li>
    <li>
        <a class='link1'></a>
        <a class='link2'>
            <div class='div1'></div>
            <div class='div2'></div>
        </a>
    </li>
</ul>

I tried like this:

$(".link1").hover(
function () {
        $(this).nextAll('.div1').css('display','block');
        ...
    },

    function () {
        ...
    }
);
like image 905
test Avatar asked Feb 20 '23 11:02

test


1 Answers

.next and .nextAll only search the "direct siblings". The element you want is a child of the link's sibling.

You can try this:

$(this).next('.link2').children('.div1')

Or, you can travese to the parent and search all decendants:

$(this).parent().find('.div1')
like image 59
Rocket Hazmat Avatar answered Feb 28 '23 10:02

Rocket Hazmat