Annoying underlines from JavaScript active links - no thanks!

Today I am facing a problem. I want to create a script that will emphasize the active link that I clicked on. I'm not sure how to do this. Can you provide me with some advice?

I have an idea, but I'm not certain if it's the best solution.

I believe I should create a CSS class for this purpose. It should look something like this:

.active {
text-decoration: underline;
}

I am planning to use the methods .addClass("active") and removeClass("active").

I wrote a simple code, but unfortunately, it doesn't work properly.

$(function(){

    $("li input").on( "click", function(e) 
    {
        $(this).addClass("active");
    });
});

Currently, I'm struggling with removing the underline from the previous element (object).

<ul>
                    <li><input type="button" onClick="Clock(0)" value="MADRID" name="MADRID"/></li>
                    <li><input type="button" onClick="Clock(0)" value="AMSTERDAM" name="AMSTERDAM"/></li>
                    <li><input type="button" onClick="Clock(4)" value="ABU DHABI" name="ABU DHABI"/></li>
                    <li><input type="button" onClick="Clock(1)" value="LONDON" name="LONDON"/></li>
                    <li><input type="button" onClick="Clock(8)" value="HONG KONG" name="HONG KONG"/></li>
                    <li><input type="button" onClick="Clock(8)" value="CHINA" name="CHINA"/></li>
                    <li><input type="button" onClick="Clock(3)" value="ATHENS" name="ATHENE"/></li>
                    <li><input type="button" onClick="Clock(8)" value="SINGAPORE" name="SINGAPORE"/></li>
                    <li><input type="button" onClick="Clock(4)" value="MOSCOW" name="MOSCOW"/></li>
                    <li><input type="button" onClick="Clock(7)" value="JAKARTA" name="JAKARTA"/></li>
                </ul>

Answer №1

Looks like you've encountered a few pesky bugs.

$(function(){
    var checkboxes = $("li input").on( "click", function(e) {
        checkboxes.not(this).removeClass('active')
        $(this).addClass("active");
    });
});

Additionally, don't forget to add the following code snippet:

.active {
    text-decoration: underline;
}

To see it in action, check out this demo on Fiddle.

Answer №2

Revise your CSS Class Definition to:

.active
{
    text-decoration: underline; /* PREVIOUSLY WRITTEN AS 'text_decoration' */
}

Substitute '_' with '-'

Answer №3

If you want to ensure that only one button becomes active when clicked.

$(function(){

    $("li input").on( "click", function(e) 
    {
       $(this).addClass("active").siblings().parent().find('input').removeClass("active");

    });
});

Additionally, there is a typo in your CSS snippet. It should read as follows:

.active{
text-decoration:underline;
}

If you wish for a specific element to remain active even after a page refresh, simply include the "active" class in the HTML markup like so:

<li><input type="button" onClick="Clock(0)" value="MADRID" name="MADRID" class="active" /></li>

Choose which list item's input you would like to have this behavior applied to.

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

Transitioning from jQuery to Prototype

After switching from a jQuery background to Prototype, I am curious if there is a chart available that shows the equivalent prototype methods for specific jQuery methods? To be more specific, I am searching for the equivalent of $('#my-id').prep ...

Updating the value within a nested object in React's useState function

Here is an example of how I am managing state using useState: const [data, setData] = useState([ { id: 1, options: [{ id: 1, amount: 0 }, { id: 2, amount: 0 }] }, { id: 2, options: [{ id: 1, amount: 0 }, { id: 2, amount: 0 }] } ]); ...

The Analog Clock is unable to access the property of null

I'm struggling to understand what's wrong here. The querySelector doesn't seem to be functioning correctly as I keep encountering the error "cannot read the property of null" on line 10 of my JavaScript code. const deg = 6; const hr = docum ...

Turning off v-navigation-drawer for certain routes in Vue.js

I currently have a v-navigation-drawer implemented in my webpage using the code below in the App.vue file. However, I am looking to disable it on certain routes, such as the landing page: <v-app> <v-navigation-drawer id ="nav" persistent : ...

How can I ensure my webpage displays properly on different devices using HTML?

I have been trying to make my code responsive by adding media queries, but I am struggling to get it to work with the current setup. I even attempted setting the width to 100%, but still facing issues. I would really appreciate a simple explanation on how ...

What Could Be Causing the Issue with My Submit Button's addEventListener Function?

At the initial stages of my project, I am encountering a puzzling issue with my addEventListener. Surprisingly, using onClick with the submit button yields the desired results and displays output in the console. However, attempts to implement addEventListe ...

Guide to populating a nested entity in Mongoose

Here are the definitions of my models: const UserSchema = new mongoose.Schema({ name: String, workspaces: [ { workspace: { type: mongoose.Schema.ObjectId, }, owner: Boolean } ] }); const WorkspaceSchema = new mongo ...

Action type is not recognized: modification

After browsing through multiple forums, I haven't found a solution that works for my particular issue with my VueJS app. The problem arises when I try to input something in my first component. Below is the code snippet: main.js import Vue from &apos ...

Learn to display multiple collections of data on a webpage using Node.js and MongoDB

Struggling with displaying multiple collections on my webpage. After extensive research, I keep encountering an error message saying "Failed to look up view in views directory." Here is the code snippet causing the issue: router.get('/', functio ...

Can you provide the Angular JS alternative for executing a POST request similar to this Curl command?

Trying to translate a Curl command into Angular JS code has been a challenge for me as a newcomer to AngularJS. This specific instance involves a mobile app built on the ionic framework, which utilizes Angular JS. The original curl command looks like this ...

The error message "Joomla! 2.5 and Virtuemart 2.0.18a - $.facebox is not recognized"

I encountered an issue while using Joomla! 2.5.8 and Virtuemart 2.0.18a where I received the error message "Typeerror $.facebox is undefined in vmprices.js (line 67)" when attempting to Add to Cart. Interestingly, switching back to the default Joomla! temp ...

Using AJAX to search through paginated pages is quite simple and effective

Currently, I have developed a JavaScript script that filters names within a list. However, a problem has arisen with pagination - as the list paginates, I am unable to retrieve names without refreshing the page. Is there a way to use AJAX to access and dis ...

Choosing specific anchors based on their corresponding div ids

Just beginning my journey with JS, looking to tackle an issue (I know, everyone says that!). My task is to create a script that can choose an anchor element and inject an <img>. Nested within a div with the id of _nwa_social_logins, there are multipl ...

Does the CSS overflow property affect the borders of an element?

Consider a situation where a "div" element has a border applied to it. When the overflow rule is set to 'hidden', any content that falls directly on the border will vanish. Is there a solution for this issue? It is crucial in my case to ensure t ...

Perform a prerender of a Vue.js 2.0 component (resembling the functionality of this.$compile in Vue 1)

I'm currently working on developing reusable components specifically for gridstack. One issue I've encountered is the absence of a simple equivalent to the this.$compile method from vue 1. I came across this example that caught my attention. Her ...

Creating two div elements next to each other in an HTML file using Django

I'm having trouble arranging multiple divs in a single line on my website. Utilizing float isn't an option for me due to the dynamic number of divs generated by Django posts. Here is the code I am currently using: Index.html <!DOCTYPE html&g ...

Utilizing Radio Buttons for Table Selection in a React Application

Currently, I am utilizing React, MUI, and Formik in my project. My task involves implementing a table where only one radio button can be selected per row. How can I achieve this functionality? If you want to take a look at my code, it's available on ...

The for loop is not receiving any values

This is puzzling me a bit. I am facing an issue with two functions in my code: 1) var revisionNumber; var $list = $('<ul>'); TFS_Wit_WebApi.getClient().getWorkItem(284) .then(function(query) { revisionNumber = query.rev; ...

Update the style class of an <img> element using AJAX

My success with AJAX enables PHP execution upon image click. However, I seek a real-time visual representation without page reload. Thus, I aim to alter <img> tag classes on click. Presently, my image tag resembles something like <img title="< ...

Activate Bootstrap tab form with the ability to include sub tabs for added functionality

I have been working on a method to open Bootstrap 3 tabs on my pages based on the URL. For example, if the URL is myurl.com#tab1 (it will open the tab with id #tab1). Here's the code I am using: $(function () { var hash = window.location.hash; ...