Ways to eliminate color from a link upon clicking a different one

I had a specific idea in mind - to create a simple menu bar where clicking on a particular link would display related content below. The chosen link should turn blue when selected. I managed to come up with the following code:

<a href="#" class="link">Link 1</a>
<a href="#" class="link active">Link 2</a>
<a href="#" class="link">Link 3</a>

a, a:visited { color:black }
a.link.active { color:blue; }

$(function() {
   $('a.link').click(function() {
       $('a.link').removeClass('active');
       $(this).addClass('active');
   });
});

Check it out on this Fiddle: https://jsfiddle.net/gHb9F/. It functions as intended, but I now want "Link 2" to be blue upon window load and change back to black once another link is clicked.

Answer №1

You have the option to trigger a click event on the second link using JavaScript,

$(function() {
  $('a.link').click(function() {
    $('a.link').removeClass('active');
    $(this).addClass('active');
  }).eq(1).click();
});
a,
a:visited {
  color: black
}
a.link.active {
  color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a href="#" class="link">Link 1</a>
<a href="#" class="link">Link 2</a>
<a href="#" class="link">Link 3</a>

Alternatively, you can also directly add the 'active' class to the second link using jQuery,

$(function() {
  $('a.link').click(function() {
    $('a.link').removeClass('active');
    $(this).addClass('active');
  }).eq(1).addClass('active');
});
a,
a:visited {
  color: black
}
a.link.active {
  color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a href="#" class="link">Link 1</a>
<a href="#" class="link">Link 2</a>
<a href="#" class="link">Link 3</a>

Another approach is to include the 'active' class directly in the HTML markup for the second link,

$(function() {
  $('a.link').click(function() {
    $('a.link').removeClass('active');
    $(this).addClass('active');
  });
});
a,
a:visited {
  color: black
}
a.link.active {
  color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a href="#" class="link">Link 1</a>
<a href="#" class="link active">Link 2</a>
<a href="#" class="link">Link 3</a>

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

Is it advisable to restrict ajax requests?

Is using ajax requests more resource-intensive than a traditional page load? Consider a basic menu with standard links where clicking a link takes you to a new page. By using ajax, I can avoid this default behavior and instead fetch the linked page' ...

connect validation of the form to a separate component from the current one

Currently, I am working on creating a searchable-dropdown component that I intend to use in multiple components. However, I am facing an issue with binding form validation to this component. For instance, I have a form for creating a user and I need to bi ...

Creating fresh texts by pairing the values in constants with variables

Make sure to carefully read the question before proceeding. I have attempted multiple commands in order to retrieve a single variable from the server [TSE](https://old.tsetmc.com/Loader.aspx?ParTree=15131F) which serves Stock Exchange information. The pag ...

Enable Component to Exceed to the Following Line

I'm having trouble getting a map of a component to move to the next line (as indicated by the yellow arrow). Currently, instead of moving below, it is squeezing the Component (the Cards). I've added background colors for clarity. Any advice would ...

JS Issue with triggering .click() function

I'm currently incorporating dropzone.js to enable file uploads. In my setup, I have a button with the id #btn and an input field with the id #input. The following code snippet is functional: $('#input').on('input', function() { ...

Retrieve the <style> tag response and inject it into the head section of the HTML using React

Although I am aware that this may not be the best practice, it seems to be the most suitable solution for this particular case. The server response contains something like this: <style id="styles">.color_txt{color:green;}</style> I am attempt ...

What are the IF conditions for multiple radio button fields using jQuery?

Click here to view the demo <form action=""> <input type="radio" name="colour" value="black"> black <br> <input type="radio" name="colour" value="white"> white <br> <br> <input type="radio" name="form" value="rou ...

How can I maintain my bottom navigation bar in Bootstrap 3 without using a 3 span icon?

Hey everyone, I'm still getting the hang of MVC as I only started using it around 3 weeks ago. Currently, I am working on a Web Application where I am trying to implement a specific feature: At the bottom of the page, I have a navbar with some Html. ...

What is the best way to customize the label of Material UI's Tab component?

Is there a way to customize the text color inside the Tab component while still allowing it to change color when selected as an active Tab? For example: <Tabs centered onChange={handleChange} value={value} textColor={'secondary'} indicatorCol ...

Which HTTP headers pertain to the loading of iframes? nuxt-helmet

Can anyone explain which security headers are associated with iframe loading issues and may prevent the iframe from being loaded? I implemented nuxt-helmet to configure security headers in my nuxt project. However, after uploading my site to a server loca ...

Tips for concealing information that is scrolled beneath a translucent layer

In the scenario where you have two overlapping divs, with the top one being transparent, the goal is to have the bottom div hide as it goes under the top transparent div when scrolling. It's important that the bottom div's visibility is not compl ...

Searching for a particular Prettier setting

Seeking a Nicer alternative. I am exploring if there is a way to format code like this without moving the first attribute to a new line: <div class="test-class" [test-binding]="true" (test-binging)="test()" ...

The fit-content max-height property is not functioning properly in Firefox

This CSS approach is effective in browsers like Chrome. p.ex1 { max-height: fit-content; height: 250px; border:solid 1px black; overflow: auto; } The goal is to utilize the full height, allowing scrolling when necessary, while also adjusti ...

Adding a component to a slot in Vue.js 3

My Goal I aim to pass the Component into the designated slot. The Inquiry How can I effectively pass the Component into the slot for proper rendering? It works well with strings or plain HTML inputs. Additional Query If direct passing is not feasible, ...

Unleash the power of jQuery by incorporating the Ajax functionality with a hover option to enhance user interactivity. Utilize the .ajax

On my website, I have a calendar displayed with dates like "11/29/2014" stored in an attribute called "data-date". The goal is to check the server for a log file corresponding to that date and change the CSS of the div on mouse hover. Here is the current ...

Utilizing Loops in a React Component's Function

(React web app development) I am currently working on a project where I need to check the stock status of products using their IDs in a JSON data loop. However, I am facing an issue with the condition checking for the ID match, as it always seems to return ...

Issues with passing parameters in JavaScript

I am facing an issue while passing multiple variables from a PHP page to a JavaScript function. Only the first parameter seems to be passed successfully. In the PHP code, the script is being called like this: <? $sdate = 0; $edate = 2; ?> <scrip ...

What causes the layout of an application to become distorted when I extend the theme in Tailwind.css?

I had the idea of incorporating an animation into my website design using tailwind.css for styling. module.exports = { content: ["./index.html", "./src/**/*.{js,jsx}"], theme: { colors: { primary: "#E51114", ...

Why isn't jQuery working properly for showing/hiding elements?

I am attempting to create a functionality where the string remove field can be toggled to show or hide using a single button, depending on its current state. Initially, it should be hidden (using the hidden HTML attribute), and upon clicking the button, it ...

Are There Other Ways to Replicate the Actions of an Anchor Link/Target Pseudo-Class?

New Inquiry: Can the onclick attribute within an <a> tag function the same way as the href attribute? In the code snippet below, I have applied <a href="#view"> to all images, and <a id="close-customlightbox" class="lb-animate" href="#!"&g ...