Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Change one element after selecting another with jQuery

I have an unordered list that looks a bit like this:

<ul>
<li id="#practice_cat-1" class="active">
<a href="#>Text</a>
</li>
<li id="#practice_cat-2">
<a href="#>Text</a>
</li>
</ul>

I also have another element on the page that I change the background of whenever I hover over the list items above. The jquery to change the background-image on hover is below:

$(function() {
$('#practice_cat-2').hover(function() {
$('#practice_page_section_left').addClass('imageshowing2')
}, function() {
// on mouseout, reset the background colour
$('#practice_page_section_left').removeClass('imageshowing2')
});
});

How can I also apply this to the LI that has the class of active?

I assume I select the element like so -- $('#practice_cat-2.active') -- but not sure the best way to say if said element is selected then change the bg image of the other element.

Hopefully that makes sense. Anyone have an idea? I haven't found a solution yet on the jquery site.

11 Answers

Hi Joseph, If you have only one LI with the class active, you could just create another handler when you hover over that LI.

Disclaimer: I am not a jQuery master, so I dont know the best way to do this :)

Regards, Joseph :)

I know some javascript but not jquery yet, so honestly that's a bit out of my reach still. Any jquery experts out there?

Thanks Joseph :)

If your goal is to change the background on ANY li with the class of active the following should get you going. If you want me to take a look at the code you are working on try putting together a Jsfiddle or Codepen and share the link.

$(function() {
var $section_left = $('#practice_page_section_left');
$('li.active').hover(function() {
$section_left.addClass('imageshowing2')}, function() {
$section_left.removeClass('imageshowing2')
});
});

Here's a Jsfiddle -- made a few modifications to the javascript and not getting anything to work yet. Maybe if you see the code you might be able to help me figure it out :)

http://jsfiddle.net/XdA4a/

Can explain the desired functionality you're trying to achieve with this? Not what you've implemented already, but what you want to happen? Would love to try and help, but a bit confused on what you need.

The unordered list is a nav menu of sorts that loads an image on the body on hover (this part is implemented) and on click loads an additional nav menu to the right and should also load an image in the body (this is the part I'm having trouble with). I suppose a link to the site would be useful in this case, so here ya go

http://www.unarch.com/practice/

Joseph the code worked as planned you just forgot to add in the jQuery library on the fiddle ( under Frameworks and Extension ). I did a little clean up to the code that should make it more extensible in the future.

Joseph - Here's a super simple example, that I made using codepen.

My code isn't as extendible as Mark's , though you might find, it's logic is easier to follow.

Mark & James, really appreciate the help but I think the code is only making the other image show on hover am I right (going to test right now just to double check)? I'm actually trying to change it when the list item is clicked on, which triggers the class of active to be applied. I already have code that applies the class of active on click... just nothing to take that and change the background.

Joseph you will want to use the jQuery click method instead of hover.

Thanks Mark