Stop Bootstrap form controls from displaying a focus glow when clicked

A virtual directory has been created where users have the ability to change file and directory names.

Users can only edit names by double-clicking:

<input type="text"
                   class="form-control border-0 p-0 mb-1"
                   onblur="this.readOnly='true';"
                   readonly="true"
                   ondblclick="handleDbClick(this)"
                   onkeydown="handleEnter(event, this)"
                   value="{{ dict.directory.current_name }}" />

However, when clicking the input field once, there is still a glowing effect around it: https://i.sstatic.net/80ucJnTK.png

The goal is for the blue glow effect to only appear when the input is double-clicked and in editable mode.

Answer №1

To disable the style (box shadow) when input is focused, you can create a class like this:

.nofocus:focus {
    box-shadow:none;
}

Then, you can enable the style in your event handlers by removing and adding the class.

For instance, add it in your double click handler:

el.classList.remove('nofocus');

Remove it when the Enter key is pressed in the keydown handler:

if (evt.keyCode === 13) el.classList.add('nofocus');

Extend the blur handler to toggle it (you could also use an isEditing variable to hold the input state and prevent hiding if the input is being edited, based on your workflow logic).

if (!el.classList.contains('nofocus')) el.classList.add('nofocus');

Demo:

.as-console-wrapper { max-height: 10% !important; bottom: 0; }
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cfada0a0bbbcbbbdaebf8ffae1fce1fc">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet">
   
    <title>Bootstrap Example</title>
    <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f2d20203b3c3b3d2e3f0f7a617c617c">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>
    <style>
      .nofocus:focus {
        box-shadow:none;
      }           

    </style>
  </head>
  <body class="p-3 m-0 border-0 bd-example m-0 border-0">

    

    <input type="text"
    class="form-control border-0 p-0 mb-1 nofocus"
    onblur="handleBlur(this)"
    readonly="true"
    ondblclick="handleDbClick(this)"
    onkeydown="handleEnter(event, this)"
    value="{{ dict.directory.current_name }}" />



  <script>
    
    let isEditing = false;

    function handleBlur(el) {
      console.log('handleBlur');
      if(!isEditing) {        
        if (!el.classList.contains('nofocus')) {
          el.classList.add('nofocus');
          el.readOnly = 'true';
        }
      } else {
        el.focus();
      }


    }

    function handleEnter(evt, el) {
      if(!isEditing) return;
      console.log('handleEnter');
      el.removeAttribute('readonly');
      if (evt.keyCode === 13) {
          el.classList.add('nofocus');
          el.readOnly = 'true';
          isEditing = false;
      }
    }

    function handleDbClick(el) {
      isEditing = true;
      console.log('handleDbClick');
      el.classList.remove('nofocus');
    }
  </script>
      
 
  </body>
</html>

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

Can you assist with transforming the html, css, and js code into a svelte format using style lang="postcss" specifically for tailwind?

I'm attempting to transform this shining button from a codepen I discovered into a svelte component, but for some reason the sparkles don't appear. I'm unsure if it's relevant, but I'm also utilizing Tailwind CSS, so my style tag h ...

Serve a dynamically generated .js file in ExpressJS when a specific route is accessed

I am working on developing my ExpressJS app, and I have a specific requirement where I need the route to return a dynamically generated .js file that will be utilized by an html <script> tag. Can anyone provide guidance on how to accomplish this ta ...

Identify the descendant of the `<li>` tag

I need help creating a interactive checklist. The idea is that when you click on an item in the list, a hidden child element should become visible. <div id="list_one"> <h2>LIST TITLE</h2> <div id="line"></div> < ...

The epoch time indicates a 12-hour difference

I keep encountering an error with the time showing as 12:00 P.M. When I receive a response in epoch time format 1454092200000, it corresponds to 1/30/2016, 12:00:00 AM GMT+5:30 $scope.shipmentDate = moment(1454092200000).format("YYYY/MM/DD hh:mm"); The ...

Tips for transforming a 9-digit uploaded data into a 10-digit number using Angular

As someone who is new to Angular, I am working with an Angular UI where users upload a CSV file containing 6 columns of data. Before invoking the .NET API, I need to modify the data in the 1st column: if a value consists of 9 digits, it should be changed t ...

I recently started exploring the world of creating discord bots using node.js, and I'm currently facing a bit of a challenge with it

As a beginner in the world of creating discord bots, I am currently focusing on developing commands. I recently followed a tutorial on Youtube which guided me through the process. The tutorial instructed me to use the following code snippet: const prefix ...

Creating a specialized Adobe DTM Page Load Rule that will exclusively trigger within an iFrame

Within my webpage, there is an iFrame containing specific steps to follow. I am seeking a way to trigger a page load rule exclusively for the content within the iFrame on the page. The URLs of my main webpage and the iFrame are distinct. Both locations h ...

Updating the iframe source without reloading the main page

Within my ASP.NET website, I have a page that contains an iframe inside an Ajax Update Panel. I am able to change the src attribute of the iframe using JavaScript, but the issue is that when the src attribute is modified, it causes the parent page to refr ...

Struggle with creating a responsive Strava Iframe (CSS/HTML)

UPDATE: My ongoing attempts to make the iframe container responsive have been focused on adjusting its size based on the intrinsic ratio of the iframe. Unfortunately, this approach has not fully worked as some elements of the iframe remain fixed in pla ...

Retrieving an array from the $.get() method within multiple nested loops

I'm currently working on a jQuery plugin that takes an array of JSON files and needs to compile them into one large array. While each part of the code works individually, I'm facing issues when trying to integrate them together and return the ne ...

Creating a color gradient for hyperlinked text when hovered over: a tutorial

How can I achieve this effect using CSS only? I want the link color to change from #00C to #000 when hovered over, with a gradient transition taking 1 second. ...

What is causing the navigation to disappear off the left side of the screen when the media query is activated?

When the screen size is less than 450px, the logo disappears in order to make room for navigation. However, the navigation then extends about 20px off the left side of the screen. View the code here This CSS code snippet appears to be relevant: @media s ...

Transmit variables to ajax callback function

I'm a beginner when it comes to AJAX, so I'm using 'Jquery Form' to help me out. I'm trying to pass the entry and path variables to the success callback. var optionsUpdate = { data: { entry: entry, path: path }, success: ...

What is the best way to import modules in Typescript/Javascript synchronously during runtime?

I have a Typescript class where I am attempting to perform a synchronous import, however, the import is being executed asynchronously. My code snippet looks like this: --------------100 lines of code-------------------- import('../../../x/y/z') ...

Despite the onsubmit returning false, the submit operation still goes through

I seem to have hit a roadblock yet again. My registration form incorporates three JavaScript functions which display the desired output when triggered by an onchange event within my HTML form. These functions generate a Bootstrap alert while the user is co ...

In Electron, methods for detecting and resolving Error TS2304: Unable to locate the name 'unknown on(event: 'ready', listener: (launchInfo: unknown) => void): this are essential for smooth operation

Encountering an issue while running npm build. Electron version: 7.1.2 TypeScript version: ^2.5.1 Target version: ES2017 Here are the details of the error. When I performed the build under the following conditions: Electron version: 6.0.0 TypeScript ver ...

What is the best way to iterate through only the selected checkboxes to calculate their

I am in need of calculating the selected checkboxes only within a table field (harga_obat*jumlah) Below is my foreach data: @foreach ($obat as $o) <tr> <td> <input id="check" type="checkbox" name="select[]" value="{{ $o->nam ...

The NodeJS req.query is providing inaccurate information

When I send a JSON from the Client-Side to a NodeJS Server, it looks like this: $.ajax({ type: "POST", dataType: "jsonp", url: "www.xyz.com/save", data: { designation: "Software Developer", skills: [ "", "ASP.NET", "P ...

Add the URL link according to the specific domain name

I am looking for a way to attach additional URL parameters to any links that contain example.com on a webpage. The current script I have only works if the link does not already have parameters attached to it. <script> document.body.innerHTML = d ...

AngularJS's support for html5mode on GitHub pages is now available

Is it feasible for GitHub pages to accommodate AngularJS in html5mode? I came across a source online suggesting that it can be done with a fallback page for 404 errors. However, this solution seems flawed as it may result in multiple 404 errors, which wou ...