Using JQuery and CSS to handle multiple hyperlink links with a single action

UPDATE: Issue resolved, thanks for the help. It is working fine now: http://jsfiddle.net/c3AeN/1/ by Sudharsan

I have multiple links on my webpage, all in a similar format like this: note: When I say 'similar format', I mean that all links share the same class but have different IDs and text.

$(".do_it").click(function(){
 // In this function, I extract the ID from the clicked link and redirect to the corresponding website
}

<a href="#" class="do_it" id="999">link1</a>
<a href="#" class="do_it" id="998">link2</a>
.
.
.

<a href="#" class="do_it" id="1">link999</a>

There are specific actions based on the ID, and the issue arises when one link is clicked, turning all links purple. Is there an easy solution to only apply the 'purple' style to the clicked link? //Apologies for any language errors

Answer №1

To ensure consistency in your website's design, style the visited links to be the same color as the regular links using CSS.

a {
    color: #00f;
}
a:visited {
    color: #00f;
}
.visitedLink {
    color: #f00; /* define your desired color here */
}

To apply the custom color on a visited link when clicked, create a class with the desired color and use jQuery to add it dynamically:

$(".mylink").click(function() {
    $(this).addClass("visitedLink");
    // include additional code here
});

Answer №2

To create unique links, make sure to assign a different href for each one:

<a href="#apple">Apple</a><br>
<a href="#banana">Banana</a><br>
<a href="#orange">Orange</a><br>
<a href="#grape">Grape</a><br>

Note: Avoid using onclick handlers that return false as it prevents the link from being visited. However, without this, scrolling may reset to the top of the page with each click.

Answer №3

You don't need to use jquery for this task, you can achieve it using just CSS like so:

a:visited{
  color : purple; // choose any color
}

Additionally, here is a complete example:

a {
    color: blue;
    text-decoration: underline;
}

a:active {
    color: yellow;
    text-decoration: none;
}

a:link {
    color: blue;
    text-decoration: underline;
}

a:visited {
    color: purple;
    text-decoration: none;
}

a:focus {
    color: red;
    text-decoration: none;
}

a:hover {
    color: red;
    text-decoration: none;
}

Answer №4

a.do_it:visited{
  text-decoration:underline;
  color : #800080;
}

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

I am curious as to why the "default + label" pseudo-class is not functioning properly on the "input" element. Specifically, I am having trouble with the "+ label" portion

":default" works fine on its own, but when combined with the "label" pseudo-class, it fails to apply. .pseudo-test input:default { box-shadow: 0 0 20px 20px red; } .pseudo-test input:default+label { color: coral; } <div style ...

Troubleshooting: My jQuery script for fading in content upon document ready is not

I'm currently working on a website where I want everything to fade in when the user enters the site. Take a look at my code: var celyLogin = $(".container"); $(document).ready(function () { /*! Fades in page on load */ $("container") ...

Hold on until the page is reloaded: React

My current setup includes a React Component that contains a button. When this button is clicked, a sidePane is opened. What I want to achieve is refreshing the page first, waiting until it's completely refreshed, and then opening the sidepane. Below i ...

Efficiently sending VueJS data to a separate script

I am in the process of transitioning an existing site to VueJS, but I have encountered a roadblock when it comes to finding the best method to accomplish this task. The site currently utilizes D3-Funnel (https://github.com/jakezatecky/d3-funnel) to genera ...

Issue with jQuery behaving unexpectedly during Ajax requests

Using jQuery/PHP/MySql, I have successfully implemented a feature to load Twitter search results on my page. The initial load is limited to the first 20 search results. As the user scrolls down the page and reaches the bottom, an additional 20 results are ...

Validate the checkbox with Vuelidate only when the specified property is set to true

I have a website login form where users may need to check a specific checkbox before logging in. Here is part of the Vue component code that handles this functionality: <script setup lang="ts"> import {ref, defineProps} from 'vue&a ...

Utilizing Regular Expressions in JavaScript to extract packages from an Android manifest document

When it comes to Android APK files, the manifest files play a crucial role: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapp" android:versionCode="1" ...

Having trouble with the JSFiddle dropdown button that is unresponsive to

I am currently in the process of developing a test drop-down menu. The open menu button functions correctly, however, the close button is unresponsive to clicking or hovering actions. Upon clicking the open menu button, it should make the other button visi ...

What is the name of this JavaScript function declaration syntax? (var) => {}

While perusing the most recent NodeJS documentation, I stumbled upon a novel approach to defining a function: fs.unlink('/tmp/hello', (err) => { if (err) throw err; console.log('successfully deleted /tmp/hello'); }); Source: ht ...

Center a vertically aligned element following a heading, with the content dynamically generated and enclosed in a fixed container

My mind is racing with this problem and I'm not sure if it's solvable, but before throwing in the towel, I thought I'd seek help from the Internet Gods. Here's a visual representation of what I'm aiming for: I want the text to al ...

Can you show me the steps to incorporate a fade-in effect?

Is there a way to incorporate a fade in effect here? $(document).ready(function(){ var myQuotes = new Array(); myQuotes[0] = "The connection of all things... "; myQuotes[1] = "Achieving greatness"; myQuotes[2] = "Your purpose is to uncover" ...

How to interact with AngularJS drop-down menus using Selenium in Python?

I have been working on scraping a website to create an account. Here is the specific URL: Upon visiting the site, you need to click on "Dont have an account yet?" and then click "Agree" on the following page. Subsequently, there are security questions th ...

Providing parameters to a helper function in one class which invokes a method in another class

I have a prototype method that looks like this: ProjectClient.prototype = { connect: function() { console.log('ARGS: ' + Array.prototype.slice.call(arguments) // This part takes a data object, a relationship string, and anoth ...

How can I ensure that my HTML form inputs are consistently displayed in the browser when using jQuery/AJAX and SQL queries without needing to

My goal is to show all textarea inputs in the browser even after a page refresh, as currently when I send data from homepage.php using jQuery/AJAX, it disappears upon refresh. The information is sent from homepage.php to data.php via the #textarea input an ...

What is the best way to move information from one table to a different one?

I have two tables: users2 and user_totals. Each of these tables has rows named clicks (impression_count), fclicks (use_count), and completed_stus (completion_count). The ones in brackets are the new additions in the user_totals table. I would like to tra ...

The UI router experiences a crash and requires a refresh after precisely six clicks

Whenever the following sequence of events occurs, my application seems to malfunction. Upon clicking on a user and assigning them a role, the page refreshes. A separate GET request retrieves a list of all users currently in the class. After selecting ex ...

Node.js Binary Search Tree - Error: Identifier Not Found

A program run through node.js has been developed to create a binary search tree with various methods like insert, remove, and print. The program is divided into two separate files: Tree.js, which exports the functions Tree() along with its methods and test ...

Assign the default value of a Vue prop to the options of its parent component

I have a child component that needs to accept a value given in the component's parent's $options object as a possible default value. In the background, the component should be able to receive its data through a prop or from a configuration. Howe ...

Puppeteer: Easier method for managing new pages opened by clicking a[target="_blank"]; pause for loading and incorporate timeout controls

Overview I'm seeking a more streamlined approach to managing link clicks that open new pages (such as target="_blank" anchor tags). By "handle," I mean: fetch the new page object wait for the new tab to load (with specified timeout) Steps to r ...

The NodeJS server encountered an issue when attempting to load the JavaScript modules

Hey everyone. I'm currently using Node.js and Express.js for the back end of my web application, but I keep running into issues with the server trying to bind references in the source code. Here's the structure of my app: src/ - static/ - ...