Using jQuery to apply the "a" class with the addClass method

The html structure below is giving me trouble:

<div class="sub">
       <a href="#" id="people">People</a>
</div>

The CSS for the "a" element is as follows:

color:#999999;
font-size:31px;

I am attempting to use jQuery to change the color by adding a class "active" with the style "color:#777!important;", but my current approach is not working. I have tried the following code:

$('.sub').click(function() {
                $('a#people').addClass('active');
            })

However, it does not seem to be functioning as expected. I have also attempted to add the class using this code:

$('.sub a').addClass('active');

If anyone has any suggestions on how to properly add a class to an "a" element, I would greatly appreciate it.

Thank you.

Answer №1

Your current solution seems to be accurate: view it in action here. If you're encountering issues, there may be other factors at play that we are not aware of.

Answer №2

The current solution you have is functioning correctly. You can view it here:

http://jsbin.com/oweye/edit

It seems that the issue might be due to not wrapping the click assignment within a document ready function, as shown below:

$(document).ready(function() {

    $('.sub').click(function() {
      $('a#people').addClass('active');
    });
 });

If this step was not followed, the handler may be assigned before the element exists. I have also made modifications to the colors in your CSS to enhance the effect.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Interacting with CSS buttons using Selenium

Currently, I am working on a test case in Java where I am using Selenium to record a series of events and then play them back on an application. Here is the code snippet I have: // *The app opens a new window* // Get handle of the main window Stri ...

What is the difference in performance between using element.class { ... } compared to just .class { ... } in CSS?

Is there any impact on performance when specifying div.class or p.class instead of just .class if a certain class will only be used on divs or paragraphs? ...

Steps for activating or deactivating the book in/book out button in an ajax table

Hello, I am currently learning about AJAX and could use some assistance. Can someone help me figure out how to enable or disable the "Book In" or "Book Out" buttons in an AJAX table based on the result of the "book in" or "book out" field? Here is my inde ...

Loading texts with the same color code via ajax will reveal there are differences among them

I am currently designing a webshop with green as the primary color scheme. Everything is functioning perfectly, but I have noticed that the text within an ajax loaded div appears brighter than it should be. The text that loads traditionally is noticeably d ...

Effortlessly Create a jQuery Page Load Effect with Fade In and Out

I am seeking assistance and guidance from anyone who can lend a helping hand. Although this may seem like a simple question to some, I am relatively new to using jQuery so I appreciate your patience. After following a tutorial here, I have implemented th ...

Stylish date picker in CSS

Here is a design mockup along with my current solution. The issue arises when two numbers are adjacent to each other (either horizontally or vertically) as they should merge, illustrated in the mockup below. For example, when selecting numbers 48-49-50, t ...

Turn off the custom CSS section in the WordPress Customizer for WordPress themes

Looking for assistance in locking or disabling the Appearance -> Customizer -> Additional CSS area on Wp-admin. I have referred to this codex: https://codex.wordpress.org/Theme_Customization_API. However, I couldn't find any hook or function to disab ...

How can I generate a bounding box with CSS?

I am a newcomer to vue.js and I have a question about creating a bounding box for any element. This bounding box resembles a view box in svg. For example, I have created a checkbox like this one: checkbox Below is the code to style the checkbox: /* checkb ...

Closing the nested accordion will collapse all sections within the accordion

I'm currently facing an issue with my nested accordions. I've been attempting to nest them in a way that doesn't require writing additional jQuery code for each new one added. As an example, I've created a jsfiddle... https://jsfiddle. ...

Automatically populate the article number field once the article name has been entered

I am currently working on a HTML <form> using PHP. Within this form, there are three <input> fields. The goal is to have the second <input> field automatically populate once the first one is filled out. This will involve triggering an HTT ...

The output of the jQuery require function does not return a function, but rather an

During one of my tests, I attempted to utilize jQuery with jsdom, but unfortunately, I cannot seem to make it work. The issue arises with the require statement for jQuery $: it returns a function when no window object is passed, but as soon as I do provide ...

Is it feasible to automate the cropping process with selenium?

Exploring a new challenge: simulating a cropping feature. I understand that replicating human cropping is impossible, but the image I intend to crop remains a fixed size each time I run the test. My goal is to establish the cropping WebElement at a constan ...

assigning a numerical value to a variable

Is there a way to create a function for a text box that only allows users to input numbers? I want an alert message to pop up if someone enters anything other than a number. The alert should say "must add a number" or something similar. And the catch is, w ...

Having difficulty refreshing the dataTable using the ajax.url.load() function during client-side processing

As a newcomer to jQuery, I have been experimenting with dataTables for client-side processing. One thing I've learned is that all the data needs to be fetched at once from the server for client-side processing in dataTables. However, I encounter situa ...

I'm looking for a way to implement a jQuery-style initialization pattern using TypeScript - how can I

My library utilizes a jQuery-like initialization pattern, along with some specific requirements for the types it should accept and return: function JQueryInitializer ( selector /*: string | INSTANCE_OF_JQUERY*/ ) { if ( selector.__jquery ) return select ...

Conceal specific image(s) on mobile devices

Currently, I am facing an issue on my website where I need to hide the last two out of six user photos when viewed on mobile or smaller screens. I believe using an @media code is the solution, but I'm unsure about the specific code needed to achieve t ...

Do you know of a more streamlined approach to formatting this SCSS code?

Currently working on a Saleor Site and diving into the world of Django and SASS for the first time. While crafting my own styling rules in SCSS files, I've noticed some repetitive code that could potentially be streamlined. It seems like there should ...

Tips for avoiding overflow while utilizing animations

In my current project, I am facing an issue with a container div that has the CSS property overflow: auto which cannot be removed. Inside this container, there is another div that toggles between showing and hiding using an animation triggered by a button ...

Having Trouble Retrieving and Updating Records in MS CRM?

My current situation involves working with Microsoft Dynamics coding for the first time, and I am in need of a solution for my code. I have established two entities called Acc and Con, where Acc serves as the parent entity and Con is the child entity of A ...

Using C# to make an AJAX request

//Custom JavaScript function function UpdateAndSaveProjectRecords() { var CompanyCode = ''; var ProjectName = ''; var ProjectDescription = ''; var ProjectType = ''; ...