Guide on Modifying the color of a selected link tag

Here is the code snippet for a link tab:

HTML

<div class="sortLinks right">
    <label>Sort by</label>
    <a onclick="javascript:SortOrder('DATE')" href="#">Date Modified</a>
    <span class="sort_sep">&nbsp;</span>
    <a onclick="javascript:SortOrder('ALPHA')" href="#">Alphabetical</a>
</div>

CSS

a:focus
{
    color:Blue;    
}

JQuery

function SortOrder(order) {
    $.ajax({
        type: "POST",
        cache: false,
        url: "@Url.Action("SecondarySkillDetails", "SkillLifeCycle")",
        data: { primarySkillID: $("#ddlPrimarySkills").val(), sortorder: order },
        success: function (data) {
            $("#content").html(data);
        },
        error: function (xhr, textStatus, error) {
           alert (error);
        }
    });
}

I am looking to change the color of the active link to blue, however, it returns to black once clicked outside. How can I prevent this from happening?

Answer №1

After making some adjustments that differ from the others, I have provided a functional example below.

Link to Fiddle

Initially, I revamped the click logic to utilize an "on" method, and then utilized it to append the class active to the active selection.

 $(document).ready(function(){
    $('.sortLinks').on('click','a',function() {
        $('.sortLinks a').removeClass("active");
        var clazz = $(this).attr('class');
        $(this).addClass('active');
        SortOrder(clazz);
    });
});

Please note that in the example provided, I have commented out the ajax call (as it would not function in the fiddle environment).

Answer №2

To achieve this using only HTML and CSS, you can utilize hidden radio buttons.

Check out this Fiddle for a demonstration

Here is the HTML code:

<label class="sort" onchange="alert('Sorting by: date')">
    <input type="radio" name="sort" value="date" checked />
    <span>Date Modified</span>
</label>

And here is the accompanying CSS:

.sort > input[type=radio] {
    display: none;   
}

.sort {
    cursor: pointer;
}

.sort > input[type=radio]:checked + span {
    color: red;
}

Answer №3

Implement the following CSS code snippet for links:

a {
color: #0077CC;
cursor: pointer;
text-decoration: none;
}
a:hover {
text-decoration: underline;
color: #0077CC !important;
}
a:visited {
color: #4A6B82;
}
a:active {
color: #6BAC28;
font-weight: bold;
text-decoration: underline;
}

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

Stop the ongoing ajax request, initiate a new one, and then resume the initial ajax call

There's some doubt in my mind about whether this can be done. During a button click event, I am making two ajax calls. The first call uses jQuery ajax post to add data to the database, and the second call does the same with a different set of data. ...

Issue with fading in! The div should fade in only when it is no longer covering the element

I am currently working with two div elements called "#Header_Info" and "article". The "article" div is static and displays information on the page, while the "#Header_Info" div expands to show extra information. However, a problem arises when the "#Header ...

Encountering issues with CSS Modules in Next.JS app while using app/about/layout.tsx

Currently, I am delving into the world of Next.js and encountering some challenges with locally scoped CSS modules. In my project, I have an about directory which contains a layout component. Strangely, the styles applied to the layout component are not b ...

Ajax encountering 404 error in ASP MVC3 website

When testing this Ajax code on my local machine, it works perfectly. However, once we deploy it to a DEV server, we encounter a 404 error. The website is built on ASP MVC3 and interacts with a SQL database without any issues elsewhere. I am new to Ajax and ...

Guide to creating a key-value pair from an array

Here is the JSON data: [["123", "456", "789", "user1"], ["987", "654", "321", "user2"]] In my code, I stored it like this: var rows = [ ["123","456","789","user1"] , ["987","654","321","user2"] ]; I am trying to figure out how to create key-value ...

Is it possible to generate multiple modal windows with similar designs but varying content?

I am facing a challenge with 140 link items that are supposed to trigger a modal window displaying a user profile for each link. The issue is that each user profile contains unique elements such as three images, a profile picture, text paragraph, and socia ...

Disappear text gradually while scrolling horizontally

There is a need to create a special block that displays fading text on horizontal scroll. However, the problem is that the block is situated on a non-uniform background, making the usual solution of adding a linear gradient on the sides unsuitable. Click ...

Discover the simple steps to generating dynamic variables with jQuery!

I am looking to dynamically create jQuery variables based on values in a loop. Here is an example of what I am trying to achieve. array=["student","parent","employee"] $.each(user_types, function( index, value ){ var dynamicType = value + "_type"; // t ...

Generate a collection of div elements as child nodes

I'm working on a project where I have multiple rows with input fields. My goal is to create an array for each row that contains the values of the inputs. For example, if I have 3 rows and the values of 'input1' are 1, 'input2' are ...

Modify the color or background color of a disabled Material UI checkbox

The disabled unchecked checkbox appears too subtle to me, and I would like to enhance it by giving it a grey background and changing the cursor style to not-allowed. I've been trying to implement these styles on the checkbox using the makeStyles, but ...

Ways to adjust the distance between logo and slider

I am using this script to showcase some products. You can find the script at: http://tympanus.net/Tutorials/ItemSlider/ When resizing the web browser from maximum size to smaller, I find that the height between the logo and shoes is too large for my likin ...

What could be causing my jQuery $(this).next().removeAttribute to not function properly?

I am currently working on a file upload form that includes buttons like "Choose file", "Upload", and "Add other file". I am facing an issue where the "disabled" attribute is successfully removed from the first Upload button upon File Input change, but it ...

The div needs to be positioned above another div, not beneath it

Just returning to the world of coding and struggling with a basic problem. Any help would be greatly appreciated. I am trying to position the 'header-top' (red) div at the top, and the 'header-bottom' (blue) div at the bottom. However, ...

Using Model-View-Controller (MVC) to seamlessly integrate jQuery with AJAX for dropdown lists and

Here's the scenario: When a user selects a category from a dropdown on the page, the second dropdown next to it should dynamically populate with all users in that category. I'm using LinqtoSQL to retrieve all the data and I have a couple of ques ...

The functionality to refresh an input field upon clicking a button is not functioning as expected

I am currently developing a small MVC.NET web application with user input functionality. The goal is to calculate and display the results in two input fields upon button click. However, I am facing an issue where the input fields remain empty after the but ...

Is it true that document.execCommand only works with buttons and not links?

Below is the code snippet I am working with: <div contenteditable="true" style="height:100px;width:100px;border:1px solid; " class="editor"></div> <a class='bold' style="height:10px;width:10px;">B</a> $(function(){ ...

Using jQuery to interact with Twitter Bootstrap modals

By utilizing Bootstrap, we have multiple divs ranging from Button 1 to Button 6. Using jQuery, a modal will pop up with two additional buttons: cancel and confirm. When the confirm button is clicked, the modal will close and the state of the respective bu ...

Clicking our way back through the loop

Looking to display a name when each item is clicked. I've assigned an ID to each element, but it seems like I'm overlooking something. In the current code, I'm thinking there should be a variable involved, but I'm uncertain: $(document ...

Populate DataGrid using an input through AJAX and jQuery technology

I've been grappling with a persistent issue that's been bothering me for days now. Here's the scenario: Currently, I have a DataGrid that is empty and an Input Text field in place that utilizes AJAX and jQuery to display a list of available ...

Troubleshooting issue with image dimensions in Angular within CKEditor content

One issue I am facing is with CKEditor, where images inserted into Rich Text Fields have their height and width attributes set in a CSS style tag. For example: <img alt="" src="https://something.cloudfront.net/something.jpg" style="height:402px; ...