tags:

views:

89

answers:

4

I wish to, using JQuery, show and hide some anchor tags as I hover over a list item.

How do you loop through the current anchors within a list item using $(this) ?

Here's what I have so far:

$(document).ready(function() {
    $('.currentlist > li').mouseover(function(event){
        // loop through each anchor tag within this list using $(this)
        // and add the .active class
    });
    $('.currentlist > li').mouseout(function(event){
        // loop through each anchor tag within this list using $(this)
        // and remove the .active class
    });
});


a .active
{
  display: block;
}

a.edit-icon
{
  display: none;
}

a.delete-icon
{
  display: none;
}

<ul class="currentlist">
    <li><a href="#" class="active">index</a><a href="#" class="edit-icon">edit</a><a href="#" class="delete-icon">delete</a></li>
    <li><a href="#" class="active">profile</a><a href="#" class="edit-icon">edit</a><a href="#" class="delete-icon">delete</a></li>
    <li><a href="#" class="active">contactus</a><a href="#" class="edit-icon">edit</a><a href="#" class="delete-icon">delete</a></li>
    <li><a href="#" class="active">findus</a><a href="#" class="edit-icon">edit</a><a href="#" class="delete-icon">delete</a></li>
</ul>
+1  A: 

Try this:

$(document).ready(function() {
    $('.currentlist > li').mouseover(function(event){
      $(this).find('a').addClass('active');
    });
    $('.currentlist > li').mouseout(function(event){
      $(this).find('a').removeClass('active');
    });
});

Where $(this) refers to hovered li and $('a', $(this)) context selector is used to find all links inside them and add/remove classes.

Sarfraz
@Sarfaz - thanks - hopefully nothing is going wrong with my copy and paste but.. its not working.
Nicholas Murray
@Nicholas Murray: I have modifed the code; please check :)
Sarfraz
@Sarfaz - I must be doing something stupid here's the actual code in two parts: <html><head><title>test</title><script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.js"></script><style type="text/css">a.active{ display: block;}a.edit-icon{ display: none;}a.delete-icon{ display: none;}</style></head><body>
Nicholas Murray
<ul class="currentlist"> <li><a href="#" class="active">index</a><a href="#" class="edit-icon">edit</a><a href="#" class="delete-icon">delete</a></li></ul><script type="text/javascript">$(document).ready(function() { $('.currentlist > li').mouseover(function(event){ $(this).find('a').addClass('active'); }); $('.currentlist > li').mouseout(function(event){ $(this).find('a').removeClass('active'); });});</script></body></html>
Nicholas Murray
@Nicholas: You have added ` class="active"` to all links remove those classes and then see.
Sarfraz
@Sarfaz - changed the order of the css and it works now - thanks!
Nicholas Murray
A: 

Something like

$(event.currentTarget).find("a.active").removeClass("active")

should do it for you.

Alex Mcp
+1  A: 

You can use .hover() to shorten the events, .find() to get the anchors and .addClass() and .removeClass() to toggle .active on and off, like this:

$(function() {
  $('.currentlist > li').hover(function() {
    $(this).find('a').addClass('active');
  }, function() {
    $(this).find('a').removeClass('active');
  });
});

You'll want to use .hover() here because mouseover and mouseout will fire when entering and exiting children, where mouseenter and mouseleave (which .hover() uses) won't).

Also your CSS needs a fix, this:

a .active
{
  display: block;
}

Shouldn't have a space, it should be like this:

a.active
{
  display: block;
}

Also, it should be moved to the end so it overrides the .edit-icon and .delete-icon definitions.

Here's a working version with all of the above changes.

Nick Craver
@Nick - wow never seen that jsFiddle before that rocks!
Nicholas Murray
@Nicholas - Are you still having issues after the fixes in this answer?
Nick Craver
@Nick - no its all good now. Thanks for the v. comprehensive answer via jsFiddle. I think JQuery questions must draw the fastest guns on the planet!
Nicholas Murray
@Nicholas - I'm confused how the other answer resolved any of this issues I outlined here, but good luck.
Nick Craver
@Nick - it was my bad that stopped @Sarfraz's answer succeeding first. Once again thanks - I voted both up.
Nicholas Murray