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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:38:03+00:00 2026-05-23T10:38:03+00:00

Hi the code I am trying to simplify is: $(document).ready(function(){ $(‘.selection0’).click(function() { $(‘.selection0’).css(‘background-color’, ‘white’);

  • 0

Hi the code I am trying to simplify is:

  $(document).ready(function(){
    $('.selection0').click(function() {
        $('.selection0').css('background-color', 'white');
        $(this).css('background-color', 'yellow');
    });
    $('.selection1').click(function() {
        $('.selection1').css('background-color', 'white');
        $(this).css('background-color', 'yellow');
    });
    $('.selection2').click(function() {
        $('.selection2').css('background-color', 'white');
        $(this).css('background-color', 'yellow');
    });
    $('.selection3').click(function() {
        $('.selection3').css('background-color', 'white');
        $(this).css('background-color', 'yellow');
    });
    $('.selection4').click(function() {
        $('.selection4').css('background-color', 'white');
        $(this).css('background-color', 'yellow');
    });
  });

I feel like I must be missing something, and that there is a way to make this much cleaner. Thanks!

Edit: I just wanted to clarify the functionality of this code. Basically, each ‘selection’ class corresponds to span tags around sentences in a paragraph. The code allows the user to highlight exactly one sentence in each paragraph by clicking on it. If it clicks on a different sentence, that sentence will be highlighted while the rest of the sentences in that specific paragraph is deselected.

  • 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-23T10:38:03+00:00Added an answer on May 23, 2026 at 10:38 am
      $(document).ready(function(){
        $('.selection0, .selection1, .selection2, .selection3, .selection4').click(function() {
            $('.'+$(this).attr('class')).css('background-color', 'white');
            $(this).css('background-color', 'yellow');
        });
      });
    

    Although it seems like that isnt what you are trying to do.

    You first set background to white and then to yellow.

    Although that’s exactly what your code does

    EDIT

    Changed the first change of the background to represent all elements with the current class in stead of just the one clicked (thanks Sean).

    The code expects the class to be the only class.

    If you can show your code I will see whether this will work or I’ll update my answer.

    EDIT

    Working example:

    http://jsfiddle.net/6Dznp/

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

Sidebar

Related Questions

Hi I'm trying to simplify this code but can't think which way to do
I'm trying to simplify some code by putting the ViewModel models into the code
I am having an issue converting type. I was trying code like this (minimal,
Hi i'm trying to process the mysql_fetch_array query below and simplify the code so
I am trying to find code that would help me simplify a curved path
I'm trying to simplify one of my homework problems and make the code a
I'm trying to simplify some inherited PHP code that assigns a value to one
Concept So, I'm trying to define a macro to simplify the following code: for
$('body').append('<style type=text/css> body {background:red;} </style>'); This doesn't make the background red. Is there away
I'm trying to simplify some code where I have an extremely long switch statement

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.