Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery select next element

I have the next menu structure.

<li class="menu-422"><a href="item-1" title="">Item 1</a></li>
<li class="menu-444"><a href="item-2" title="">Item 2</a></li>
<li class="menu-449"><a href="item-3" title="">Item 3</a></li>
<li class="menu-452"><a href="item-4" title="">Item 4</a></li>

In this structure the items (the a-tag) have background. I want to change the background of the next item on hover event. If I'm over the Item 2 I want to change the background of the Item 3. I tried it with jQuery, but I didn't find the appropriate code.

jQuery("#mymenu li a").hover(function() {
  jQuery(this).next("li a").css('background', 'none');
}

I tried it with nextAll, with full selector path, but I can't select the next element.

like image 693
danyg Avatar asked Aug 28 '11 18:08

danyg


People also ask

Which selector selects the P element that are next to each div elements?

The ("element + next") selector selects the "next" element of the specified "element". The "next" element must be placed right after the specified "element" to be selected.

How do I find a specific parent in jQuery?

The parents() is an inbuilt method in jQuery which is used to find all the parent elements related to the selected element. This parents() method in jQuery traverse all the levels up the selected element and return that all elements.

What is closest tr in jQuery?

jQuery closest() Method The closest() method returns the first ancestor of the selected element. An ancestor is a parent, grandparent, great-grandparent, and so on.

How can I get next sibling in jQuery?

jQuery next() Method The next() method returns the next sibling element of the selected element. Sibling elements are elements that share the same parent. The DOM tree: This method traverse forward along the next sibling of DOM elements.


1 Answers

Upon hover, you want to go up to the parent li, then use next() to get to the next li, then find('a') inside of that li:

$("#mymenu li a").hover(function() {
    $(this).parent().next().find("a").css('background', 'none');
});
like image 121
Joseph Silber Avatar answered Oct 17 '22 02:10

Joseph Silber