Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying An Element Already Activated By Another Selector

I have a bit of a complicated problem.

There are two elements here:

(1). When selecting the $(".steps li") I want the whole <li> to change color to rgb(66, 81, 95). Then it has to change back to how it was before.

This part I have accomplished already, using .data().

The second part is the tricky part:

(2). When selecting an <a> in the very same <li> I want the color to stay the same and the underline to be applied. So I want the "World Assembly" text to stay green, be underlined, and have the rest of the <li> be the white, inactivated color.

Is there a way to do this using callbacks in the hover function?

I need (1) and (2) to work at the same time.

I tired to hover on just the $(".steps li a") but that doesn't work because for the first part to work, the class has to be removed.

Anyway, I am unsure about this. Any advice would be appreciated.

Code is below:

CSS:

    html, body {
    background: #000;
    color: #e7e7e7;
    font-family:"Helvetica", "Arial", "Bitstream Vera Sans", "Verdana", sans-serif;
    margin:0;
    padding:0;
}
a {
    color: rgb(66, 81, 95);
    text-decoration:none;
}
a:hover {
    /*color:#a0a0a0;*/
    text-decoration:none;
}
.wa a {
    color: rgb(68, 118, 67);
}
.steps {
    width:400px;
    margin:0 auto;
    text-align:left;
    line-height: 200%;
}
.steps a:hover {
    text-decoration: underline;
}
.steps li:hover {
    cursor: pointer;
    color: rgb(66, 81, 95);
}

JQuery:

$(".steps li").hover(function () {
    var the_class = $(this).children().attr("class");
    $(this).data('class', the_class);
    console.log(the_class);
    $(this).children().toggleClass(the_class);
}, function () {
    $(this).children().attr("class", $(this).data('class'));
});

Edit: I actually had to solve this using $.data() twice, because in my locally hosted code I ended up having to add more anchor tags in the list, all with their own colors.

It now works like this:

 $(".steps li").hover(function () {
    var the_class = $(this).children().attr("class");
    $(this).data('class', the_class);
    $(this).children().toggleClass(the_class);
}, function () {
  $(this).children().attr("class", $(this).data('class'));
});

$(".steps li a").hover(function(){
     $(this).parent().parent().toggleClass('notHover');
     $(this).parent().attr("class", $(this).parent().parent().data('class'));
     }, function()
     {
     $(this).parent().parent().toggleClass('notHover');
     $(this).parent().removeClass($(this).parent().parent().data('class'));
     });
like image 884
LSD Avatar asked Jan 25 '15 21:01

LSD


2 Answers

Here are a couple solutions to your problem (I used some hex values, because RGB is typically not used for the web).

Solution 1: Add/Remove a class on hover: JSFiddle

jQuery:

$(".steps li a").hover(function() {
    $(this).parent().toggleClass("color");
});

CSS:

.color {
    color: #e7e7e7 !important;
}

Solution 2a: Alter your jQuery: JSFiddle

jQuery:

$(".steps li a").hover(function() {
    $(this).parent().css("color", "#e7e7e7");
});
$(".steps li a, .steps li").mouseleave(function() {
    $(this).parent().css("color", "");
});

CSS:

.steps li:hover, .steps li:hover a {
    cursor: pointer;
    color: rgb(66, 81, 95);
}

.steps li a:hover {
    color: #447643;
}

Solution 2b: Alter your jQuery again: JSFiddle

jQuery:

$(".steps li a").hover(function() {
    $(this).parent().attr("style", "color: #e7e7e7");
}, function() {
    $(this).parent().attr("style", "");
});

CSS:

.steps li:hover, .steps li:hover a {
    cursor: pointer;
    color: rgb(66, 81, 95);
}

.steps li a:hover {
    color: #447643;
}
like image 185
TylerH Avatar answered Nov 15 '22 10:11

TylerH


Just toggle a class on the parent <li> when <a> is hovered.

Then a new set of rules can cover the li and a colors based on the class

$(".steps li a").hover(function){
     $(this).parent().toggleClass('aHovered');
});

.steps li.aHovered{
  color : white
}

.steps li.aHovered a{
  color : green
}
like image 30
charlietfl Avatar answered Nov 15 '22 09:11

charlietfl