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

What is the process of converting a string to XML in jQuery versions 1.3 and earlier? Additionally, how can you append a node to an

Currently, I am extracting an XML string from a hidden field and the task at hand is to append a new node to this XML. The first step involves converting the XML string into an XML object. However, since I am limited to using jQuery version 1.3, the pars ...

Popup showing values that are not defined

I've encountered an issue with tooltips on my bar graph. The tooltips should display the values of boarding and alightings corresponding to each stopname column, but I'm seeing undefined values like this Below is my code snippet: <!DOCTY ...

Is it possible for Vue Router to remember the scroll position on a route and return to the same position when navigating back?

My Vue Router is not saving the scroll position and always loads at the top of the page (0, 0). Any suggestions on what could be causing this issue? Here is my current router code setup: const scrollBehavior = (to, from, savedPosition) => { if (saved ...

Steer clear of using inline styling when designing with Mui V5

I firmly believe that separating styling from code enhances the clarity and cleanliness of the code. Personally, I have always viewed using inline styling (style={{}}) as a bad practice. In Mui V4, it was simple - I would create a styles file and import i ...

jQuery AJAX Triggered Only Once in Callback Function

I am facing an issue with the jQuery get function within my updateMyApps. The function works fine when called directly after it is declared. It successfully loops through the data and appends elements to the DOM. However, when I submit the form #addapplic ...

Adjust the angle of an object precisely using a transform upon hovering over a designated button

I am looking to design a menu that always keeps the arrow pointing at the button I hover over with my cursor. If I don't hover on any button, then it should stay in the position of the last button I hovered over. HTML: <html lang="en"> <hea ...

Tips for adjusting the height of a fixed-size child element on the screen using only CSS and JavaScript restrictions

I am faced with a challenge involving two child elements of fixed size: <div class="parent"> <div class="static_child"> </div> <div class="static_child"> </div> </div> .parent { border: 1px solid black; dis ...

What is the process for turning off deep imports in Tslint or tsconfig?

Is there a way to prevent deep imports in tsconfig? I am looking to limit imports beyond the library path: import { * } from '@geo/map-lib'; Despite my attempts, imports like @geo/map-lib/src/... are still allowed. { "extends": &q ...

What causes JavaScript parseFloat to add additional value in a for loop implementation?

I am facing a challenge where I need to convert an array of strings into an array of decimal numbers. The original array of strings is structured like this: var array = [" 169.70", " 161.84", " 162.16", " 176.06", " 169.72", " 170.77", " 172.74", " ...

PHP code to insert a advertisement div after every 5 rows of results

Is there a way to dynamically insert a div after every fifth row on a result page? I am currently using a script that displays 15 rows from a database with each pagination. Here is my complete script: <?php $sql = "SELECT COUNT(id) FROM table"; ...

Table that can be scrolled through

Back in 2005, Stu Nichols shared a technique for creating a fixed header with scrolling rows in a table on this site. I'm curious if there are any newer methods or improvements to achieve the same effect, or is Stu's approach from 2005 still con ...

Ways to eliminate empty values from an array in JavaScript

I need help deleting any null elements from my array [ [ null, [ [Array], [Array] ] ] ] I am looking to restructure it as [ [[Array],[Array]], [[Array],[Array]], [[Array],[Array]] ] If there are any undefined/null objects like : [ [[Array],[]], [[A ...

implementing a vertical separator in the main navigation bar

Is there a way to insert a vertical divider after each li element on the main menu? <ul class="nav navbar-nav"> <li><a href="#"><i class="fa fa-home"></i> <span class="sr-only">(current)</span></a>< ...

Tips for guaranteeing that functions within .then() are finished before moving on

Here is an example of using $.when().then(). $.when(setLineDetails(reportId, reportLine)).then(function(data) { console.log("Completed setting line details"); setHeaderDetails(reportId); }).then(function(data) { cons ...

I'm curious if it's possible to perform background tasks using React Native Expo with the example I have in mind

Is there a way to perform background tasks in React Native Expo? I am looking to make a POST request every 5 seconds and log the results. Can someone guide me on how to achieve this using the example from this site? I would like to modify the given exampl ...

Is it necessary to insert a thread sleep in HtmlUnit before clicking a button?

I have been experimenting with HtmlUnit to extract scores from the BBC Sports website Upon loading the page, it initially displays Premier League scores. To view scores for other leagues, one must use a dropdown menu and click the 'Update' butto ...

When the AJAX function is successfully completed, proceed with another operation

I am currently using a function that utilizes AJAX to load content (ajax.load_mainmenu). Once the content is loaded, I need to perform additional actions. Due to restrictions, I cannot call these actions directly on "success", so I need to execute them aft ...

How about checking the memory usage in Javascript?

Similar Question: Looking for a Javascript memory profiler I am curious about determining the memory consumption of variables in JavaScript. Could it be done at all? ...

Can you confirm the mobile type, please? Using JavaScript to display a div only once based on the mobile type

Is there a correct way to determine the type of mobile device I'm using? Are there alternative methods to check for the mobile type? Take a look at my approach in the code below. How can I test this using a tool? Does anyone have insights on checki ...

"Uncaught ReferenceError: $ is not defined - $function()" error in JavaScript/jQuery

When attempting to execute a JavaScript/jQuery function, an error is encountered when using Firebug: $ is not defined $(function()". The issue arises from the placement of the JavaScript code within a file named core.js that is referenced by index.php. W ...