Keep the link underlined until a different button is pressed

I need help with a webpage that displays a list of partners organized by categories.

When clicking on sidebar categories, the link remains underlined. However, if you click anywhere else on the page, the link becomes inactive.

CSS:

button:hover { 
    text-decoration: underline;
    color:#000;
}
button:focus { 
    text-decoration: underline;
    color:#000;
}
button:active { 
    text-decoration: underline;
    color:#000;
}
button:visited { 
    text-decoration: underline;
    color:#000;
}

HTML:

<button id="showall" style="border:none;">All Deals</button>
<button id="show" style="border:none;">Business</button>
<button id="show2" style="border:none;">Design</button>
<button id="show3" style="border:none;">Development</button>
<button id="show4" style="border:none;">Marketing</button>

Is there a way for the link to remain underlined unless other category buttons are clicked?

Appreciate any assistance!

Answer №1

By using jQuery, you have the ability to add a class with an underline on click events.

var $buttons = jQuery('button');
$buttons.on('click',function() {
  jQuery(this).toggleClass('active').siblings('button').removeClass('active');
})
button:hover, button:focus, button:active, button:visited, .active { 
    text-decoration: underline;
    color:#000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="showall" class="active" style="border:none;">All Deals</button>
<button id="show" style="border:none;">Business</button>
<button id="show2" style="border:none;">Design</button>
<button id="show3" style="border:none;">Development</button>
<button id="show4" style="border:none;">Marketing</button>

A more JavaScript-focused approach would look something like this...

var buttons = document.getElementsByTagName("button");

function setActive(el) {
  for (var i = 0; i < buttons.length; i++) {
      if (buttons[i] == el) {
        el.classList.toggle("active");
      } else {
        buttons[i].classList.remove('active');
      }
    }
}

for (var i = 0; i < buttons.length; i++) {
  buttons[i].addEventListener("click", function() {
    setActive(this);
  });
}
button:hover, button:focus, button:active, button:visited, .active { 
    text-decoration: underline;
    color:#000;
}
<button id="showall" class="active" style="border:none;">All Deals</button>
<button id="show" style="border:none;">Business</button>
<button id="show2" style="border:none;">Design</button>
<button id="show3" style="border:none;">Development</button>
<button id="show4" style="border:none;">Marketing</button>

Answer №2

If you want to enhance the appearance of your links, consider adding a custom class like "sidebarbuttons" to each one and implementing the following JavaScript code for onclick events:

$(".sidebarbuttons").on("click", function() { 
   $(".sidebarbuttons").not(this).each(function() { 
      $(this).css("text-decoration","none");
   });
   $(this).css("text-decoration","underline");
});

By utilizing this script, you can remove underlines from all elements assigned with the class "sidebarbuttons" and apply an underline only to the specific link that is clicked.

Answer №3

Only one link should be active at a time. Follow these steps to achieve that:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index950</title>
    <style type="text/css">
        .keepShow {
            text-decoration: underline;
            color: #000;
        }
    </style>
    <script src="~/Scripts/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $(".ap").click(function (event) {
                $(".ap").removeClass("keepShow");
                $(this).addClass("keepShow");
            })
        })
    </script>
</head>
<body>
    <div>
        <button id="showall" class="ap" style="border:none;">All Deals</button>
        <button id="show" class="ap" style="border:none;">Business</button>
        <button id="show2" class="ap" style="border:none;">Design</button>
        <button id="show3" class="ap" style="border:none;">Development</button>
        <button id="show4" class="ap" style="border:none;">Marketing</button>
    </div>
</body>
</html>

Answer №4

Using basic vanilla JavaScript:

const buttons = document.querySelectorAll(".wpb_wrapper button");
buttons.forEach(function(button, index){
  button.addEventListener('click', function(){
    // Remove active classes from all other buttons first
    document.querySelectorAll(".wpb_wrapper button").forEach(function(btn){
      btn.classList.remove("active");
    });
    this.classList.toggle("active"); // Toggle active class on current button
  });
});

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

Monitoring separate upload progress within $q.all() in AngularJS

I recently started using the angular-file-upload module created by danialfarid (https://github.com/danialfarid/angular-file-upload) and I must say, it's been a great experience so far. After successfully integrating it into my wrapper service for RES ...

Bypass license and credit comments when compressing JavaScript with YUIcompressor

When using YUICompressor to minify JavaScript, is there a method to retain license or credit comments? Are there specific commenting characters to use, or a flag in YUICompressor for this purpose? Thank you, Grace ...

What is the best way to adjust the placement of this object so it is anchored to the left side?

https://i.sstatic.net/k8aF1.png I'm struggling to position one of my divs more to the left within a flex container. No matter what I try, changing the class of the div doesn't seem to have any effect. Here's the code snippet: import React f ...

Connecting to a MongoDB or Mongoose database dynamically using Node.js

I am currently working on developing a multi-tenant application where each client will have its own dedicated database. Here is my scenario: I have created a middleware that identifies the client based on their subdomain and retrieves the necessary datab ...

Creating a custom ID for a Firebase POST request

Is it possible to assign a custom ID in Firebase when using the POST method? For example: { "users": { "user_one": { "username": "jdoe", "password": "123" } } } I'm currently working on a Vue.js project and I want to save ...

Having trouble retrieving AJAX data [PHP]

When a form on my homepage (index.php) is submitted, it opens a randomly generated URL in a new tab. This random URL runs a script named run.php. <form action="/run.php" method="POST" target="_blank"> <input type="hidden" id="idgen" name="idg ...

Shut down the angular modal

I am currently learning AngularJS and I find myself working on an application that utilizes both angular modal and jqGrid. (I understand this may not be the ideal setup, but for now, I have to make it work with both.) The content of my modal is loaded usi ...

Ensuring the accuracy of URLs using JavaScript

I am developing a form with an input field for entering a URL and a submit button. I want to incorporate JavaScript validation to show an alert box when a correct URL is entered. Is it achievable using JavaScript? If so, can you please guide me on how to i ...

Skipping MongoDB in loop operations in a Node.js environment.The original text was modified to

Apologies for the beginner question (The following code is related to express framework and mongoose DB) I am attempting to iterate through the array 'Users' which contains usernames. Then, I am trying to match them in the mongoose database to r ...

Use jQuery to set different background images for multiple divs at random time intervals

I am facing a challenge with displaying multiple divs next to each other on my website, each with a unique background image. I have written a script that randomly changes the background images at different time intervals, but currently all the images chang ...

Divide the parsed rss findings into separate parts

I am in the process of developing a project that involves aggregating job listings from various websites by parsing their RSS feeds. I am utilizing rss-parser for this purpose. Each feed has its own format for the title field, resulting in varying structu ...

"Activate the parent window by navigating using the accesskey assigned to the href

I have integrated a bank calculator tool into a website. The calculator opens in a new window, but I am encountering an issue. Users need a shortcut to open the calculator multiple times. I have discovered the accesskey feature, which works the first tim ...

Displaying a clock using PHP

Seeking a way to display server time on my website, I have successfully implemented client-side time using JavaScript. How can I now showcase the server time? Are there any ZendFramework classes that can assist with this? Currently, I am utilizing the fo ...

Transfer an Array of Objects containing images to a POST API using Angular

Looking to send an array of objects (including images) to a POST API using Angular and Express on the backend. Here's the array of objects I have: [{uid: "", image: File, description: "store", price: "800"} {uid: "f ...

Error in AngularJS: Unable to access property 'get' as it is undefined

Upon examining my App, I found that it is structured as follows: var app = angular.module('cockpit', ['tenantService', 'ngMaterial', 'ngMdIcons']); The controller associated with my App appears like this: angula ...

Center 3 elements horizontally using Flexbox, ensuring the center element is aligned properly

Is it possible to center 3 elements using flexbox on a page? The middle element should be centered on the page, while the left and right elements can vary in width. Please refer to the image below: https://i.sstatic.net/IVdz8.png I am not certain if this ...

Selenium WebDriver in Java is able to detect hidden elements by checking if they are displayed or not

This snippet of HTML code contains a Form: <div id="bodyLeftBlock" class=""> <form id="signUpForm" class="" method="post" action="/en/signup/post/" novalidate="novalidate" style="display: none;"> <input class="" type="hidden" va ...

Issues with rendering Vue 3 component, visible in the elements tree but not displaying on the screen

Recently, I delved into Vue to update one of my components with the new syntax in order to grasp Vue 3 better. However, after the modifications, the component that had been functioning perfectly before now seems to be failing to render properly. The origi ...

Having trouble with UseSelector in React Redux?

I am currently working on a exercise to better understand the react-redux process, but I seem to have hit a roadblock. Any assistance would be greatly appreciated. One interesting observation is that when I subscribe and log the store into the console, it ...

Exploring Angular controller inheritance and the ability to override methods from a superclass

Is it possible to call a function on the superclass controller when extending a controller in Angular and overriding a function? To illustrate with an example in Java: class Foo { void doStuff(){ //do stuff } } class FooBar extends Fo ...