Is it possible to eliminate the border of an HTML element when clicked, while still keeping the border intact when the element is in

I am currently developing a project with an emphasis on Web accessibility.

Snippet of Code:

function removeBorder(){
li=document.getElementById("link");
li.classList.add(".remove")
}
body{
  background:#dddddd;
}
p:focus{
  border:1px solid red;
}
li{
  list-style-type:none;
  font-size:1rem
  padding:30px;
}
a{
text-decoration:none;
}
a:focus{
  border:1px solid red;
  border-radius:2px;
}

a:hover{
  background:orange;
}
.remove{
border:none;
}
<html>
  <head>Borders
  </head>
  <body>
  <p tabindex="0">
  Click one the page. Then use TAB to navigate the list items</p>
<ul aria-role="list">
  <li  aria-role="listitem"><a id="link" onclick="removeBorder()" tabindex="0" href="#">Item One</a></li>
 <li  aria-role="listitem"><a tabindex="0" onclick="removeBorder()" href="#">Item Two</a></li>
  <li aria-role="listitem"><a tabindex="0"  onclick="removeBorder()"  href="#">Item Three</a></li>
 </ul>  
  
  </body>
 
</html>

I have two distinct user groups.

1. Regular users:

Upon hovering over elements like 'li', I observe an orange background color.

Issue: Upon clicking on the element, a red border appears.

Is there a way to display borders only when focused by tab and not when clicked? How can I eliminate the borders upon clicking?

2. Keyboard-only Users:

No issues arise while focusing with tab as the borders appear in red as expected.

Answer №1

function addBorderEffect(){
li=document.getElementById("link");
li.classList.add(".highlight")
}
body{
  background:#dddddd;
}
p:focus{
  border:1px solid red;
}
li{
  list-style-type:none;
  font-size:1rem
  padding:30px;
}
a{
text-decoration:none;
}
a:focus:hover{
  border:1px solid red;
  border-radius:2px;
}

a:hover{
  background:orange;
}
.highlight{
border:2px solid blue;
}
<html>
  <head>Borders
  </head>
  <body>
  <p tabindex="0">
  Click on the page, then use TAB to navigate through the list items</p>
<ul aria-role="list">
  <li  aria-role="listitem"><a id="link" onclick="addBorderEffect()" tabindex="0" href="#">Item One</a></li>
 <li  aria-role="listitem"><a tabindex="0" onclick="addBorderEffect()" href="#">Item Two</a></li>
  <li aria-role="listitem"><a tabindex="0"  onclick="addBorderEffect()"  href="#">Item Three</a></li>
 </ul>  
  
  </body>
 
</html>

Combining the focus and hover effects simultaneously creates a visually appealing result.

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

Sending a collection of text inputs from a web form and saving them in MongoDB

I have been attempting to store an array of strings from my HTML form into my database (MongoDB). Here's the HTML form for creating a new class: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"& ...

How can I prevent getting stuck in a never-ending React re-render cycle?

I always believed that placing any form of setState within a useEffect call would lead to an endless re-render loop since the useEffect gets triggered on every render. Surprisingly, in my Next.js application, everything seems to be functioning well without ...

How to Disable a Button with JavaScript in a Vue.js Application Without Using jQuery

I'm currently in the process of transitioning old jQuery code to JavaScript for a Vue.js application. function DisableSubmit() { submitButton.prop('disabled', true); submitButton.attr('data-enabled-value', submitButton.val ...

Tips for transferring data to the next page with JavaScript AJAX

I am working on a webpage that includes an html select element <pre> $query = mysql_query("select * from results"); echo "<select id='date' onchange='showdata()' class='form-control'>"; while ($arr = mysql_fetch_a ...

Retrieving information from an array and displaying it dynamically in Next.js

I've been diving into the Next.js framework lately and I've hit a roadblock when it comes to working with dynamic routes and fetching data from an array. Despite following the basics of Next.js, I'm still stuck. What am I looking for? I ne ...

What is the best way to update image CSS following a rotation using JavaScript?

Discover a neat CSS trick for always centering an image in a div, regardless of its size or aspect ratio. <style> .img_wrap { padding-bottom: 56.25%; width: 100%; position: relative; overflow: hidden; } #imgpreview { display: bl ...

Tips for implementing a CSS Flexbox layout with varying div heights

I am working on a page that features five unique panels, each with its own distinct height and width. I have been considering implementing CSS Flexbox to ensure these panels stack properly as the page becomes responsive. However, one major issue I've ...

Creating components in reactjs using the render function

Just a quick query – I've been diving into react js recently. Typically, when we create a component in React, we include the HTML template within the render function. I've noticed that most examples consist of small components with minimal HTM ...

Choosing an option from the dropdown menu with the help of the incredible HTMX framework

I attempted the provided solution but unfortunately, it did not yield the desired outcome. Upon checking the HTMX log, it appears that everything is set up correctly. Can anyone point out what mistake I might be making in this scenario? document.getElem ...

Is there a valid substitute for using hot links with images?

Currently, I am instructing a group of individuals on the intricacies of web development with a specific focus on AngularJS. As part of an exercise that I designed for them, they are required to fetch data from the freely accessible pokeapi.co. One interes ...

Using Three.js to rotate a camera-followed sphere in the scene

Currently, I'm developing a Three.js scene with a toy concept where I aim to track a sphere using a camera [demo]. However, I am facing an issue wherein I can't seem to get the sphere to "roll" without causing the camera to also rotate along with ...

Difficulties in Configuring Testacular Integration with Jasmine and Angular

I'm in the process of setting up a unit testing environment for my project and encountering some difficulties. Specifically, I am attempting to utilize Testacular with Jasmine to test my AngularJS code. One of the challenges I am facing involves a mo ...

Puppeteer failing to detect dialog boxes

I'm attempting to simulate an alert box with Puppeteer for testing purposes: message = ''; await page.goto('http://localhost:8080/', { waitUntil: 'networkidle2' }); await page.$eval('#value&apos ...

The global class variable encounters an error when trying to assign the value of "socket" to it

Within my class constructor, I am setting up a Socket connection and then storing the socket parameter in a global class variable (this.socket_variable = socket) so that I can access it across all functions in the class. CODE const { Server } = require(&q ...

Assign a value to a text input using React

Whenever the closeEmail function is triggered or called, I need to set the email.emailAddress as the value of the textfield. I'm fairly new to React, what is the syntax or method to achieve this? Any suggestions? #code snippet <div style={{ disp ...

Sending JSON Data over URL

I am facing a challenge with passing data between two HTML files. My initial solution involved sending the data through the URL using the hash and then parsing the link using JSON.parse(window.location.hash.slice(1));. This method worked for a few attempts ...

Creating a jQuery variable in version 1.9.1 results in an error, whereas in version 1.8.3 it does not succeed

Recently updated to version 1.9.1 and encountered some technical difficulties. Despite that, everything works fine except for one thing: var $newthumbs = $(' <div id=\"car-7\" class=\"thumbnail_car thumbnail span2&bso ...

Control and manage AJAX POST requests in the controller

I'm currently working on implementing an ajax post request feature in my project. The goal is to have a button on my html page trigger a javascript event listener, which then initiates an ajax post request to receive some text data. However, I seem to ...

Looking for assistance with PHP if statement and troubleshooting iFrame issues

Using the PHP if statement below on my website: <?php if(empty($_GET['tid'])) echo '<iframe src="http://tracking.mydomain.com/aff_goal?a=33&goal_id=47" scrolling="no" frameborder="0" width="1" height="1"></iframe>'; ...

What methods does the W3C CSS Validator use to recognize a CSS3 document?

Help Needed! Can someone tell me how to specify a CSS3 file for validation by the W3C? In HTML5, we use <!DOCTYPE html>, but what should we use in a CSS file? Whenever I try to validate my CSS file containing CSS3 elements like @font-face and box- ...