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

  • Home
  • SEARCH
  • 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 7616379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:52:22+00:00 2026-05-31T02:52:22+00:00

I have multiple elements on a page that have matching classes. The classes are

  • 0

I have multiple elements on a page that have matching classes. The classes are built by a variable so I can’t hard code the css into a stylesheet. The elements that make up the page will constantly change and so I need some javascript to find the element’s class and then apply some css for that class. It’s on a site with Jquery, Drupal (views) if that helps at all…

I’m part of the way there. I’ve written the following script:

$(document).ready(function(){
$("a").hover(function() {
   var myClass = $(this).attr("class");
   $(.$myClass).hover(function(){
      $(this).css({
      border : '3px solid red',
      color : 'red'
      });
   });
});
});

Now if I put in there an ‘alert’, I see that it is capturing the class of the element that is currently being hovered over. But I can’t get it to then find all of the elements on the page with that class and apply the css. To add some info, the two elements that I am trying to adjust are a small thumbnail image which I would like to add a border around, and the title of a linked node which I would like to change color.

Any help is much appreciated.

  • 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-31T02:52:23+00:00Added an answer on May 31, 2026 at 2:52 am

    The problem you’re having is two-fold (at least), but effectively you’re declaring the variable:

    var myClass =  $(this).attr("class");
    

    And then trying to access that variable with a different name, .$myClass, when it should be:

    $(.myClass)
    

    Also, the period for the selector is not part of the variable name, it’s a string, and as such must be quoted and concatenated:

    $('.' + myClass)
    

    Further, you’re not assigning behaviour to those other elements on hover of those elements, but on hover of the a element you’re currently hovering. So your

    $("a").hover(function() {
       var myClass = $(this).attr("class");
       $(.$myClass).hover(function(){
          $(this).css({
          border : '3px solid red',
          color : 'red'
          });
       });
    

    Should be:

    $("a").hover(function() {
       var myClass = $(this).attr("class");
       $('.' + myClass).css({
          border : '3px solid red',
          color : 'red'
          });
       });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

  I understand that elements can have multiple classes: .rfrsh-btn { background-image:url(../../upload/rfrsh_nb_grey.png); ... }
I have a html page with multiple similar elements which can be edited in-place:
I have a page that contains multiple checkbox input elements. Toggling these alter the
I have a page that contains multiple textarea elements. These textareas and the IDs
I have an ASP.NET 3.5 page which has multiple span elements with ID containing
I have source XMLfiles that come in with multiple root elements and there is
I have an element that can have multiple types (not my design). The element
I have a block-level HTML element that contains multiple block-level and inline-level elements, e.g.
I have multiple dl elements on a page. At the end of each one
I am building a page that will have multiple instances of a tag such

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.