Create a single button that controls checkboxes with the help of HTML, CSS, SCSS, and Bootstrap 4

Is there a way to use only HTML, CSS, SCSS, and Bootstrap 4 to make all checkboxes default to the "on" condition when a button is clicked? Typically, this functionality would be achieved with JavaScript, but due to constraints, I am exploring alternative solutions. If anyone has tackled this challenge before, please share your approach.

        <div class="welcome-left-btn">
         <label class="switch">
          <input type="checkbox" name="chk" checked>
          <span class="slider round"></span>
         </label>
        </div>
        
        <div class="welcome-left-btn">
         <label class="switch">
          <input type="checkbox" name="chk" checked>
          <span class="slider round"></span>
         </label>
        </div>

        <button id="defaultbtn" class="btn" type="submit">Defaultbtn</button>


        .switch {
         position: relative;
         display: inline-block;
         width: 40px;
         height: 23px;
         }
         .switch input {
         opacity: 0;
         width: 0;
         height: 0;
         }
         .slider {
         position: absolute;
         cursor: pointer;
         top: 0;
         left: 0;
         right: 0;
         bottom: 0;
         background-color: #ccc;
         -webkit-transition: .4s;
         transition: .4s;
         }

         .slider:before {
         position: absolute;
         content: "";
         height: 19px;
         width: 19px;
         left: 2px;
         bottom: 2px;
         background-color: white;
         -webkit-transition: .4s;
         transition: .4s;
         }

         input:checked + .slider {
         background-color: #3861fb;
         }

         input:focus + .slider {
         box-shadow: 0 0 1px #3861fb;
         }

         input:checked + .slider:before {
         -webkit-transform: translateX(17px);
         -ms-transform: translateX(17px);
         transform: translateX(17px);
         }
         .slider.round {
         border-radius: 25px;
         }

         .slider.round:before {
         border-radius: 50%;
         }

         
    

Answer №1

If you want to achieve a similar visual effect without using JavaScript, consider experimenting with the :target selector.

One workaround is to modify your HTML structure by avoiding the use of the "button" tag within the "a" tag and setting the class to "btn" if Bootstrap is being used.

.switch {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 23px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}
/* CSS Code continued... */
<section id="visual_off">
  <div class="welcome-left-btn">
    <label class="switch">
            /* Additional HTML code goes here... */
        </label>
  </div>

  <a type="submit" href="#visual_on"><button>ON</button></a>
</section>
<section id="visual_on">
  <div class="welcome-left-btn">
    <label class="switch">
            /* Additional HTML code goes here... */
        </label>
  </div>

  <a type="submit" href="#visual_off"><button>OFF</button></a>
</section>

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

Tips for creating a hidden tab that only appears when hovered over

When hovering over the link tag .cat-link a.navlink, each subcategory tab .subcategories div is displayed. However, I would like to hide all tabs initially and have them appear only when hovered over with the mouse. Once the mouse is removed, I want the t ...

Generating JSON data from Dom elements within a specified `div` container

My goal is to extract all the elements from within a div element, which may consist of multiple inputs, checkboxes, radiobuttons etc. After that, I want to convert the id or name and value attributes into a format that can be read as JSON. I attempted to ...

What could be causing the undefined status of my checkUser() function?

I have implemented a brief script on my signup page to define the function checkUser(user) at line 6. In the code section at the end of the HTML for the sign up form, I included an inline script onBlur='checkUser(this) within the <input> named ...

Incrementing numbers with jQuery append autoincrement

I am working with a dynamic list of input fields, each one with a unique incremental name. Here's an example structure... <input type="text" name="values[0]" value="some text"> <input type="text" name="values[1]" value="some other text"> ...

What is the best way to dynamically update or display unique CSS styles when a service is invoked or provides a response in AngularJS using JavaScript

Seeking to display a unique CSS style on my HTML FORM prior to invoking a service in my code and then reverting back after receiving the response. I have implemented the use of ng-class to dynamically add the class when the boolean activeload1 is set to tr ...

Maximizing values entered into form fields

Looking for a way to extract the highest number from a set of input fields in an HTML form using JavaScript? Take this example: <input id="temp_<strong>0</strong>__Amount" name="temp[<strong>0</strong>].Amount" type="text" valu ...

"Enhance your webpage with a captivating opaque background image using Bootstrap

I'm new to exploring Bootstrap and I am currently experimenting with options for displaying content with a semi-transparent background image. Currently, I am using a "well" but I am open to other suggestions. I have managed to place the image inside t ...

What is the best way to present the new segment?

Having trouble with creating new sections or content after the homepage? The issue is that the new content doesn't appear on a new page but rather on the homepage itself. An example of this problem can be seen on this CodePen project, where an h1 sect ...

Creating an Editor for Input Text Field in HTML: A Step-by-Step Guide

In the vast landscape of JS libraries that can achieve this function, like Trumbowyg and more. However, prior to my rails project displaying that slim version, I need to ensure JavaScript is properly escaped! Therefore, I need to create an editor using o ...

MUI component with a stylish gradient border

I'm struggling to locate the alternative for the border-image-source in CSS My objective is to create a button component with a gradient border ...

The 'in' operator cannot be used in Jquery to search for 'display' in a value that is undefined

This is the HTML code snippet I have: <div class="V_CPp Elss <?php echo $Show; ?>"> <span class="ViewC">View more comments</span> </div> <di ...

When switching between classes, it is not possible to apply a different value to the same CSS property

I am currently working on a project that involves toggling a class on a ul tag with an id of sideNav using JavaScript. In this scenario, I have set the left attribute of the id to 0, while the same attribute in the class has a value of -100%. After sever ...

What is the most effective method for preserving RichText (WYSIWYG output)?

I am currently using a JavaScript-based rich text editor in my application. Could you suggest the most secure method to store the generated tags? My database is MySQL, and I have concerns about the safety of using mysql_real_escape_string($text);. ...

What causes my unordered list to vanish when I use the CSS float: right property?

Can anyone explain why my ul element disappears when I apply the float: right property in CSS? I'm currently working on the navigation for a website project. You can view the code snippet on CodePen by following this link: https://codepen.io/maximo ...

Creating an image rollover effect when hovering between two images

I am currently working with React and trying to change 2 images by hovering over them. Here is what I have accomplished so far: Card.js <div className="image-wrapper"> <img src={sprites.front_default} className="image" a ...

Capturing variable data without retaining the information entered

I recently received some code that seems to be malfunctioning, and I'm struggling to pinpoint the issue. The code in question involves a hyperlink (.control_button) that triggers the opening of a form (#convert-form) within a lightbox. Within this for ...

Display button in Django template based on user's group level

In my application, there are 3 groups with different permissions: viewer, editor, and creator. I need to display the appropriate buttons based on these permissions. For viewers, they can only see lists and details. Editors have viewer permissions plus th ...

Display or conceal elements by utilizing ng-show/ng-hide according to specific conditions

Here is the code snippet I am working with: <input class="form-field form-control" type="text" name="Website" ng-model="vm.infodata.Website" placeholder="Website Address" maxlength="50" required ng-pattern="/^(www\.)?[a-zA-Z0-9_&bs ...

Create a table within the B-Col element that automatically adjusts its size to match the column width using Vue-Bootstrap

Within my Vue component, I have the following code snippet: <b-modal ...> <b-row> <b-col sm="12"> <table > <tr v-for=... :key="key"> <td><strong>{{ key }}: </str ...

Sidebar Text Remains Visible Despite Collapsed Sidebar

I'm working on implementing a sidebar shrink effect when collapsing it using a combination of custom CSS, jQuery, and Bootstrap. The custom CSS and jQuery handle the shrinking effect, while Bootstrap is used for styling purposes. However, I am facing ...