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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:00:33+00:00 2026-05-24T02:00:33+00:00

How can I create different classes on my links so they can show different

  • 0

How can I create different classes on my links so they can show different images when you click on them?

The idea is that the links has its own color and when it is active there must be a little arrow with the same color.

This is what I have so far:

HTML:

<ul>
    <li class="red"><a href="#">Link one</a></li>
    <li class="blue"><a href="#">Link two</a></li>
</ul>

CSS:

.red {background-color:#f00; }
.red_bnt { background-image:url(image/red_bnt_pil.png); }

.blue {background-color:#00f; }
.blue_bnt { background-image:url(image/red_bnt_pil.png); }

jQuery:

<script type="application/x-javascript">

$('.red').click(function(){
    $('.red').removeClass('red_bnt');
    $(this).addClass('red_bnt');
});

</script>

And then repeat it for the blue as well.

But it is not working …

what am I doing wrong?

  • 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-24T02:00:33+00:00Added an answer on May 24, 2026 at 2:00 am
    $('.red').click(function(){
        $('.red').removeClass('red_bnt');
        $(this).addClass('red_bnt');
    });
    

    Your jQuery is saying that when an element with the class “red” is clicked, remove class “red_bnt” from all elements with the “red” class and then add red_bnt to the clicked element. Since the blue button doesn’t have the class red, it never even enters the equation. Instead, add a common class to the buttons and then a use a common class name, such as “active”, which would activate the background CSS.

    I’d change your CSS to:

    .red.btn {background-color:#f00; }
    .red.hover,
    .red.active { background-image:url(image/red_bnt_pil.png); }
    
    .blue.btn {background-color:#00f; }
    .blue.hover,
    .blue.btn.active { background-image:url(image/blue_bnt_pil.png); }
    

    The HTML to:

    <ul>
        <li class="red btn"><a href="#">Link one</a></li>
        <li class="blue btn"><a href="#">Link two</a></li>  
    </ul>
    

    and the jQuery to:

    <script type="application/x-javascript">
    $(function() {
        $('li.btn').click(function(){
            // Add "active" to the clicked element and
            // get all siblings and remove "active" from them
            $(this).addClass('active').siblings().removeClass('active');
        });
    
        // Use this for hover
        $('li.btn').hover(function() {
            $(this).addClass('hover');
        }, function() {
            $(this).removeClass('hover');
        });
    });
    </script>
    

    Here’s a jsFiddle for it: http://jsfiddle.net/FS4Du/. I used the colors darkred and dodgerblue in place of your images.

    Note: I used the hover class instead of just reusing active to avoid having to handle the case where the element that was clicked on (the active element) is then hovered over.

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

Sidebar

Related Questions

Can I create a component with Wix that has files in different directories/subdirectories? Or
Can c++ create array of pointers to different classes dynamically loaded from dll? ps.without
I want to create a method sum that I can call on different types,
In SQL Server 2005, we can create similar tables in two different ways. We
How can I create a new Exception different from the pre-made types? public class
I have many tables with identical schemas but different names. Can I create a
I need to create a Word template where I can choose from different paragraphs
I want to create a website with various users. The users can have different
Are there different limitations as to how many connections (sockets) that can be created
I'm trying to create function that will do this same thing for 3 different

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.