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

Unchecking the select-all checkbox in Ag-Grid after updating the row data using an external button

In my ag-grid setup, I have implemented checkboxes in the first row to allow users to select individual rows. Additionally, there is a "select all" checkbox in the header for easy selection of all rows with a single click. To create the select all checkbox ...

Is it possible to divide a column in an HTML table into two separate

I am currently working with an array of elements that I need to iterate over. For each element, I create a <td></td> tag. When dealing with 10 elements, it results in a table with one column and 10 rows. Is there a method, using either HTML o ...

The left margin class in Bootstrap seems to be having trouble functioning properly within a React

Navbar <div className="collapse navbar-collapse" id="navbarSupportedContent"> <ul className="navbar-nav ml-auto"> <li className=&quo ...

Adding graphs dynamically to Dygraphs allows for real-time updates and interactive data

I recently started using Dygraphs for one of my projects and I am still learning how to work with it. After reading through the documentation and looking at a few examples, I noticed that the constructor for Dygraphs requires a div element where the graph ...

Retrieving server information using AJAX in React

I've been attempting to utilize an AJAX call in order to fetch server data for my React Components. However, I'm encountering difficulties when it comes to displaying it with React as I keep receiving this error: Uncaught TypeError: Cannot read ...

Depressed tomato ( Using the Image .load method with jQuery )

My poor tomato is feeling sad because it's not resizing properly. When I don't include the "load" function, some of my thumbs won't resize, but if I do include it, then the other ones won't resize. startDiv.find('img').each( ...

A mysterious underscore suddenly materializes within Font Awesome icons when used in HTML

Currently, I'm working on the footer section of a webpage using HTML and Bootstrap 5.3.0. I encountered an issue where an underscore appears next to the icons when I add them. I suspect that this is due to the white spaces between the elements, which ...

first item's grandchild selection

https://jsfiddle.net/jg69c3yf/1/ I don't have a lot of experience with CSS. My goal is to target the div with the class arrow-right, but only if it's inside the first child of a div with the class active. In this sample code, Only item 3 shoul ...

Determine the number of visible li elements using jQuery

Currently, I am utilizing a jQuery script to count the number of li elements in my HTML code. Here is an example of the setup: HTML: <ul class="relatedelements"> <li style="display:none;" class="1">anything</li> <li style="disp ...

Adding a contact form to a slider: A step-by-step guide

Currently, I am faced with the challenge of placing my form on a slider in such a way that the slider appears to be running in the background of the form. When using position absolute, I find myself having to apply excessive margins and top pixels due to t ...

Bootstrap modal not displaying in full view

I recently ran into some issues while using a jQuery plugin with my bootstrap modal on my website. After implementing jQuery.noConflict(), I encountered a problem where the program no longer recognized $, forcing me to replace all instances of it with jQue ...

Combine two JSON objects which share a common attribute

I am currently working with two JSON feeds. One feed contains the basic information about a course, while the other contains more administrative details. Here is an example to illustrate what I mean. FIRST {"courses": {"course":{"id":"4","title":"Usi ...

There appears to be a CSS problem cropping up on the right side of my website

Kindly pay a visit to the following URL: dev.dgconcepts.com.pk https://i.stack.imgur.com/sBC4Dm.jpg I am puzzled by the fact that whenever we swipe left or right on mobile responsive, the website shifts slightly on both sides. I am unable to identify ...

When you click the "Select All" button in a JavaScript dialogue box, all of the checkboxes on the main page will automatically be selected

After clicking a button on the main page to add a SKU, a new window opens with a search field, as shown in the image below. In this pop-up window, there is a "Check All" button that, when clicked, selects all checkboxes on both the pop-up window and the ma ...

Continuously executing a series of JQuery functions or iterating through them repeatedly

Struggling with a jQuery animation loop issue that has me stuck. I need the animation to repeat continuously, but it's not working despite trying setInterval. Can anyone assist? Here's the fiddle link: https://jsfiddle.net/8v5feL9u/ $(document). ...

How can I make a CSS transition activate only on width increases, not decreases?

My element's width changes under different conditions, and I've implemented CSS with a transition for the width: .element-class { transition: width 500ms ease 0ms; } However, I only want the transition to occur when the width is decreasing. ...

Using Twitter Bootstrap to set a minimum number of lines for thumbnail captions

I currently have a carousel on my website created using Bootstrap that displays 4 columns of thumbnails. You can view the carousel here. When navigating to the third page of the carousel, you will notice that the container increases in height to accommodat ...

Utilizing interpolation for a CSS class defined in an external file within Angular 2

Is it feasible to send a variable to a CSS class in an external CSS file within Angular 2, such as: import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', sty ...

Verifying PHP session through AJAX

How can I execute an AJAX request to run an INSERT query? The query requires a value from a PHP session, but the JavaScript request does not recognize that value. Why is this happening and what steps should I take? See some of the code below: JS: $(&apo ...

Altering data in a concealed dynamic HTML form

I have a hidden form on my webpage that gets populated dynamically when a button is clicked. I want to be able to change the values within this form dynamically. Initially, the form looks like this: <form id="zakeke-addtocart" method="pos ...