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 5998365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:23:46+00:00 2026-05-23T00:23:46+00:00

I am trying to figure out a way to create a class to group

  • 0

I am trying to figure out a way to create a class to group together a set of effects that performs the same effects but just different color. I am a procedural guy and OOP is something I am still learning and need some guide.

I am trying to create a ‘tag’ lookalike shape using ‘a’ tag that changes the font and background color when mouseover, mouseout or when clicked. But I want to reuse the code for many other color variation so I hate to keep typing the code over and over again.

How do I group the function like a Jquery plugin?

**CSS**

a.cbSizeTag {
display: inline-block;
padding: 4px 4px 4px 4px;
margin: 0 2px 6px 0;
background: #F5F5F1;
    text-decoration:  none;
}

a.cbShipmentTag {
display: inline-block;
padding: 4px 4px 4px 4px;
margin: 0 2px 6px 0;
background: #F5F5F1;
text-decoration:  none;
}

**Jquery**

$('.cbSizeTag').mouseover(function() {
$(this).css('background-color','#076EA0')
$(this).css('color','#FFF')
});

$('.cbSizeTag').mouseout(function() {
if ($(this).hasClass('selected')){
    $(this).css('background-color','#076EA0')
    $(this).css('color','#FFF')
} else {
    $(this).css('background-color','#F5F5F1')
    $(this).css('color','#4B94BF')
}
});

$('.cbSizeTag').click(function() {
$(this).toggleClass('selected');
if ($(this).hasClass('selected')){
    $(this).css('background-color','#076EA0')
    $(this).css('color','#FFF')
} else {
    $(this).css('background-color','#F5F5F1')
    $(this).css('color','#4B94BF')
    }
 });

$('.cbShipmentTag').mouseover(function() {
$(this).css('background-color','#B80000')
    $(this).css('color','#FFF')
});

$('.cbShipmentTag').mouseout(function() {
if ($(this).hasClass('selected')){
$(this).css('background-color','#B80000')
$(this).css('color','#FFF')
} else {
    $(this).css('background-color','#F5F5F1')
$(this).css('color','#4B94BF')
}
});

$('.cbShipmentTag').click(function() {
$(this).toggleClass('selected');
if ($(this).hasClass('selected')){
$(this).css('background-color','#B80000')
$(this).css('color','#FFF')
} else {
$(this).css('background-color','#F5F5F1')
$(this).css('color','#4B94BF')
}
});

**HTML**

<a href="javascript:void(0)" class="cbSizeTag">S</a>
<a href="javascript:void(0)" class="cbSizeTag">M</a>
<a href="javascript:void(0)" class="cbSizeTag">L</a>
<br>
<a href="javascript:void(0)" class="cbShipmentTag">DHL</a>
<a href="javascript:void(0)" class="cbShipmentTag">FEDEX</a>
<a href="javascript:void(0)" class="cbShipmentTag">Free</a>

So if I were to create another 10 functions with different colors, I would have create 10 sets of CSS, 10×3 sets of Jquery code and I think there must be a better way to write it. Can anyone help with a class-template example so I can learn?

Thanks.

  • 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-23T00:23:46+00:00Added an answer on May 23, 2026 at 12:23 am

    Instead of binding this to a specific class, you can use a custom attribute(data-startColor, data-endColor) with the value of the start and end color, then bind a single block of this code to all elements that match $("[data-startColor]"), and access the custom colors by $(this).attr("data-startColor") and $(this).attr("data-endColor") inside the events you binded.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out the best way to create a class that can
I'm trying to figure out the best way to create a class whose sole
I'm trying to figure out a way to create a new class instance when
I am trying to figure out the best way to create a view like
I am trying to figure out the best way to create a navigation menu
I'm trying to figure out a way to take a Ruby array and create
I'm trying to figure out a way to format a date string that sits
I am currently trying to figure out the best way to create a .xls
I'm trying to figure out the correct way to create an AsyncTask to retrieve
I am currently trying to figure out what the best way is to create

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.