Maintain button concentration while utilizing multiple button collections

My goal is to create multiple custom button groups, where the focus remains on the clicked button within each group and does not shift when clicking on buttons in other groups.

To achieve this, I have structured the button groups as follows:

<div class="btn-group" role="group" aria-label="Isolate Button group">
  <button type="button" class="btn btn-secondary">Left</button>
  <button type="button" class="btn btn-secondary">Middle</button>
  <button type="button" class="btn btn-secondary">Right</button>
</div>

<div class="btn-group" role="group" aria-label="Another Isolate Group">
  <button type="button" class="btn btn-secondary">Upper</button>
  <button type="button" class="btn btn-secondary">Center</button>
  <button type="button" class="btn btn-secondary">Bottom</button>
</div>

Currently, when clicking on a button such as Upper, it loses focus if another button like Left is clicked. I am searching for a solution, perhaps a custom class or attribute, that can maintain the focus on the selected button.

Answer №1

Discover Bootstrap Radio Toggle Buttons

Even with the utilization of JavaScript, it seems that achieving that specific behavior might be unattainable. The occurrence of focus events is limited to one element at a time. It appears that you desire a scenario where only a single button within a group is selected, which is essentially known as toggling and can indeed be implemented using JavaScript across a collection of elements.

<button> elements by themselves do not exhibit this behavior without the intervention of JavaScript. In Bootstrap, it is JavaScript's responsibility to enable certain class combinations and elements to function in unconventional ways.

To fulfill your requirement, you may opt for utilizing <label> tags along with <input type='radio'>.

  1. <input> should precede a <label>
    <input> <label></label>
    
  2. Add class='btn-check', a unique #id, assign a grouping name to the <input>, and set the type to 'radio'
    <input id='b1' class='btn-chk' name='grp1' type='radio'>
    
  3. Include class='btn', [for] attribute with the value corresponding to the #id of the <input>
    <label class='btn' for='b1'>TEXT</label>
    
  4. Repeat steps #1 through #3 ensuring that each #id is unique and every [for] matches accurately.
  5. Enclose all these elements in a <div>, <section>, etc., and give it a class of 'btn-grp'.
    <div class='btn-group'>
      <!-- All <input>s sharing the same `name` 
           alongside their associated <label>s 
      -->
    </div>
    
  6. Repeat steps #1 through #4 making sure that each name for each <input> matches appropriately within its designated .btn-grp container, and differs from any other name existing outside that particular .btn-grp.
    <div class='btn-grp'>
      <input name='grp1' ...>
      <input name='grp1' ...>
      <input name='grp1' ...>
    </div>
    
    <div class='btn-grp'>
      <input name='grp2' ...>
      <input name='grp2' ...>
      <input name='grp2' ...>
    </div>
    

In this setup, the key is not the focus but rather a property or attribute called checked. Within CSS, it can be referenced using the pseudo-class SELECTOR:checked, while in JavaScript, it is accessed via NODE.checked as a property and also as an attribute via NODE.toggleAttribute('checked') or in HTML <TAGNAME checked>.

Note: The demo below showcases toggling behavior purely through HTML. No JavaScript has been included, only the Bootstrap stylesheet was utilized.

<!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="b7d5d8d8c3c4c3c5d6c7f78299879986">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">

  <title>Radio Button Groups</title>
</head>

<body>
  <main class="container">
    <section class='col'>
      <section class='row'>

        <div class="btn-group mt-5 mb-2" role="group" aria-label="Primary Button Group">

          <input id='p1' type='radio' class="btn-check" name='primary'>
          <label class="btn btn-primary" for='p1'>Left</label>

          <input id='p2' type='radio' class="btn-check" name='primary'>
          <label class="btn btn-primary" for='p2'>Middle</label>

          <input id='p3' type='radio' class="btn-check" name='primary'>
          <label class="btn btn-primary" for='p3'>Right</label>

        </div>
      </section>

      <section class='row'>

        <div class="btn-group mt-2" role="group" aria-label="Secondary Button Group">

          <input id='s1' type='radio' class="btn-check" name='secondary'>
          <label class="btn btn-secondary" for='s1'>Upper</label>

          <input id='s2' type='radio' class="btn-check" name='secondary'>
          <label class="btn btn-secondary" for='s2'>Center</label>

          <input id='s3' type='radio' class="btn-check" name='secondary'>
          <label class="btn btn-secondary" for='s3'>Bottom</label>

        </div>

      </section>
    </section>
  </main>

</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

Employing a graphic as a backdrop stretched across several cards

If I have the following cards: https://i.sstatic.net/LAeGQ.png (View implementation in tailwind play) Also, if I have this chart (Example chart) https://i.sstatic.net/jP7QB.png Is it possible to create a similar effect of using that chart as a backgro ...

Creating a page turn effect during the loading of a page within an iframe

Is there a way to create a page turn effect similar to reading a book for an iframe that is loading dynamic content? Any suggestions would be greatly appreciated. Link: ...

Aligning text in the middle of two divs within a header

Screenshot Is there a way to keep the h4 text centered between two divs? I want it to stay static regardless of screen resolution. Currently, the icon and form remain in place but the h4 text moves. How can I ensure that it stays in one spot? <!doctyp ...

Creating a navigation bar with a logo: A step-by-step guide

I'm trying to create a subnav below my navbar with the text aligned to the right and the logo positioned on the left. However, I'm facing issues with moving the text to the right and adjusting the position of the logo. How can I move the logo dow ...

Automatically altering variable without manual intervention

Hey there, I'm encountering a strange issue with the form below echo "<td class='contact-delete'> <form method='post' action='update_record.php'>"; echo "<button type='submit' value='" . $ ...

The Bootstrap Menu displaying multiple submenus experiences a glitch where the first submenu is duplicated

https://i.sstatic.net/Kr0q2.png My HTML markup includes a Bootstrap-4 menu with four items. Each menu item has a nested submenu, and I plan on adding more levels to it. The submenu of the first item displays correctly as "Logistics, Logistics, Logistics ...

Issue with SVG compatibility in Mozilla Firefox

Just starting out with SVG and here is my code: <div style="width:650px;"> <svg xmlns="http://www.w3.org/2000/svg" version="1.1"> <rect width="640" height="200" style="fill:#ddd;"></rect> <polyline points="0 200,20 200, 40 200, ...

When utilizing dynamic binding within *ngfor in Angular 4, the image fails to display properly

I'm encountering an issue with an <img> tag that isn't behaving as expected in my code snippet: <div *ngFor="let familyPerson of userDataModel.family" class="col-md-6 col-lg-4 family-member"> <div class="fm-wrapper"> ...

Clicking a button to reset a json file

I have a task management program and I would like to implement a feature that allows users to reset the tasks to a predefined set of JSON data within the JSON file by simply clicking a button. For instance, if the JSON file contains multiple tasks, clickin ...

Jsoup is ensuring that iframe content is properly rendered and secured

Having some source HTML that needs parsing <div> <iframe><script>alert('hello')</script></iframe> </div> After using Jsoup to parse and printing the body HTML, this is what I get. <div> <iframe>& ...

Creating JavaScript Objects from HTML Form data with the help of serializeJSON

Struggling extracting HTML form data into the required JSON format for server communication. Despite following a guide, I can't get the output right. Managed to extract question keys and values, but labeling is tricky. Current Output: {"Question1":" ...

Is there a way to transfer a value to another page using POST in PHP without utilizing an Input Tag?

Is there a way to pass a value to another page using POST in PHP without using an Input Tag? I attempted this method but it was unsuccessful. <?php include("database/config.php"); include("database/opendb.php"); include("func ...

Bootstrap relies on jQuery for its JavaScript functionality, so jQuery must be loaded before using Bootstrap's JavaScript

I encountered an issue while trying to load the Bootstrap library, consistently receiving this error message: Uncaught Error: Bootstrap's JavaScript requires jQuery Even though I have ensured that jQuery is loaded before attaching the Bootstrap li ...

Ways to insert a divider into the menu items of a Kendo Toolbar split button

Kendo UI 2015.2.805 Is there a way to insert a separator line between menu items on a toolbar's split button dropdown? I am familiar with adding a separator line between toolbar buttons, but I am having trouble achieving the same effect for the menuB ...

Accordion jQuery failing to display tabs

I have a question about using 2 jQuery accordions with tabs inside them. Everything seems to be working, but when I try to expand the second accordion, the two tabs inside don't render properly. I'm not sure what the issue is. Any help on this wo ...

Guide on triggering a button click event to display an alert message with jQuery

I'm having trouble getting the alert message to appear. The rest of my jQuery code is functioning properly, so I assume I linked the library correctly. Could the placement of the code in the document be causing the issue? Here's the HTML: <b ...

My Ajax request is hitting a snag - the success function isn't functioning as expected

Having an issue with the success function in my ajax call. It doesn't seem to be working as expected. Check out the code snippet below: var data1 = { "name": namedata[0], "email": namedata[1], "mobile": namedata[2], "company": namedata[3], "message" ...

Lost in a sea of confusion with ember-cli-postcss and autoprefixer

I'm currently working on an ember build that incorporates autoprefixer. My issue arises from the fact that the output location for 'assets/application-name.css' has shifted from its normal path to 'app/styles/app.css', and I would ...

Ways to remove content that exceeds the boundaries of a div element

I am facing a challenge with a parent div that has a specified size. Inside this parent div, there are other child divs, and if any of those child divs start displaying their content outside of the parent div, I want to completely remove that div. I do not ...

Struggling to access custom.css for Bootstrap 4

Below is my customized _custom.scss: // Adjustments to Bootstrap // // Customize default values by copying variables from `_variables.scss` to this file // without altering the original source files. //$body-bg: $gray-dark; //$body-color: $gray-light; ...