Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7521399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:13:45+00:00 2026-05-30T02:13:45+00:00

Only just starting out with jQuery and I’m sure this is basic stuff for

  • 0

Only just starting out with jQuery and I’m sure this is basic stuff for those that know, but I can’t get it working.

I have a very simple and repetitive script that I’m trying to streamline, that changes the background colour of a background div, when certain links are hovered. i.e. each link has an associated background div.

$('div#navigation a.A').hover(function(){
$('div#bg-nav .A').addClass('bg-active');
},function(){
$('div#bg-nav .A').removeClass('bg-active');
});;

$('div#navigation a.B').hover(function(){
$('div#bg-nav .B').addClass('bg-active');
},function(){
$('div#bg-nav .B').removeClass('bg-active');
});;

This is repeated for each link. Links and the background divs are created dynamically by the CMS so manually updating this script every time a page is added isn’t ideal. And I can’t use pure CSS because of where the links are in relation to the divs within the page structure.

As there is an obvious pattern, (the background divs have the same class as the trigger links), I figured it must be possible to use the class of the link being hovered to affect the relevant div, passing the variable onto the selectors, something like below, but as a newb I can’t get it working!

$(document).ready(function() {
var linkClass = $(this).attr("class");
$("div#navigation a").hover(function() {
$('div#bg-nav.'+ linkClass).addClass('bg-active');
},function(){
$('div#bg-nav.'+ linkClass).removeClass('bg-active');
});;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:13:47+00:00Added an answer on May 30, 2026 at 2:13 am

    I think this should work. Also take a look at Density 21.5’s answer for details.

    $('#navigation a').hover(function () {
        $('#bg-nav').find('.'+ $(this).attr('class')).toggleClass('bg-active');
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just starting out with jQuery and this problem seems to be impossible to me.
I just learned (the hard way) that Java Component s can only have one
I think this may be a very simple question, but I am only just
I'm just starting out with JQuery in a Drupal environment. I have some thumbnails,
I am only just starting out with iPhone application development and have been doing
I only just recently discovered that Visual C++ 2008 (and perhaps earlier versions as
I am trying to use this in my page class. I only just started
I just wonder if it is possible to only use CSS but not javascript
I'm just starting out in ASP.NET MVC (and web applications in general, as I've
I am just starting out with ADO.net Entity Framework I have mapped two tables

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.