0

I'm not sure how to access this element with jQuery.

When the user hovers over the link inside the image, I want the class hidden to be removed.

This is just one list menu item of many, so I need a way to do it with $this

<ul class="menu">
    <li>
        <ul>
            <li class="hidden">
                <div class="container">
                    <div class="one"></div>
                    <div class="two"></div>
                    <div class="three"></div>
                    <div class="four"></div>
                    <div class="five"></div>
                    <div class="six"></div>
                </div>
            </li>
            <li>
                <img src="images/sample.svg" /><a href="#">Sample</a>
            </li>
        </ul>
    </li>
</ul

In vain I tried:

$('.menu>li>ul>li:last-child>img>a').hover(function() {
    $(this).parent().parent().child().removeClass('hidden');
}, function() {
    $(this).parent().parent().child().addClass('hidden');
});

2 Answers 2

2

Use closest() and prev()

$('.menu [href]').hover(function() {
  $(this).closest('li').prev('li').removeClass('hidden');
}, function() {
  $(this).closest('li').prev('li').addClass('hidden');
});
.hidden {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<ul class="menu">
  <li>
    <ul>
      <li class="hidden">
        <div class="container">
          <div class="one"></div>
          <div class="two"></div>
          <div class="three"></div>
          <div class="four"></div>
          <div class="five"></div>
          <div class="six"></div>
        </div>
      </li>
      <li>
        <img src="images/sample.svg" /><a href="#">Sample</a>
      </li>
    </ul>
  </li>
</ul>

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

4 Comments

I think something like $('.menu').find('[href]').hover(function() {...}); would work better, for a more explicit location for the href. Do you guys agree?
@Ted, honestly I did not see your answer until after I posted. When I was typing out my answer, you know how it is, it just shows a gray bar with '1 answer has been submitted`
@AmmarCSE lol no worries. I removed mine since there's no need for duplicates, and your answer has better explanations :)
@CollinGraves -- no doubt, I just used the OP's assuming there was a good reason for it, but there may not have been a good reason for it.
0
$('.menu').find('[href]').hover(function(){
  $(this).closest('li').prev('li').removeClass('hidden');
  }, function(){
  $(this).closest('li').prev('li').addClass('hidden');
  });

Comments

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.