Increasing the size of a bootstrap checkbox - javascript

I am creating a simple check box with the code from the official bootstrap documentation:
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck" name="example1">
<label class="custom-control-label" for="customCheck">Check this custom checkbox</label>
</div>
I am wondering how to make the checkbox larger than it currently is? I have tried editing the css of customCheck and changing the height and width properties, but that does not increase the size of the box.

I added classes like *-sm, *-md, *-lg, *-xl for the bootstrap switch.
Here I made all resolutions switch with one #mixin ( #mixin is very similar to JS functions but it does not return anything ).
For Bootstrap 4
custom-switch-sm, custom-switch-md, custom-switch-lg, custom-switch-xl
SCSS: https://codepen.io/nisharg/pen/VwLbYvv
CSS: https://codepen.io/nisharg/pen/mdJmywW
LIVE SNIPPET (CSS)
/* for sm */
.custom-switch.custom-switch-sm .custom-control-label {
padding-left: 1rem;
padding-bottom: 1rem;
}
.custom-switch.custom-switch-sm .custom-control-label::before {
height: 1rem;
width: calc(1rem + 0.75rem);
border-radius: 2rem;
}
.custom-switch.custom-switch-sm .custom-control-label::after {
width: calc(1rem - 4px);
height: calc(1rem - 4px);
border-radius: calc(1rem - (1rem / 2));
}
.custom-switch.custom-switch-sm .custom-control-input:checked ~ .custom-control-label::after {
transform: translateX(calc(1rem - 0.25rem));
}
/* for md */
.custom-switch.custom-switch-md .custom-control-label {
padding-left: 2rem;
padding-bottom: 1.5rem;
}
.custom-switch.custom-switch-md .custom-control-label::before {
height: 1.5rem;
width: calc(2rem + 0.75rem);
border-radius: 3rem;
}
.custom-switch.custom-switch-md .custom-control-label::after {
width: calc(1.5rem - 4px);
height: calc(1.5rem - 4px);
border-radius: calc(2rem - (1.5rem / 2));
}
.custom-switch.custom-switch-md .custom-control-input:checked ~ .custom-control-label::after {
transform: translateX(calc(1.5rem - 0.25rem));
}
/* for lg */
.custom-switch.custom-switch-lg .custom-control-label {
padding-left: 3rem;
padding-bottom: 2rem;
}
.custom-switch.custom-switch-lg .custom-control-label::before {
height: 2rem;
width: calc(3rem + 0.75rem);
border-radius: 4rem;
}
.custom-switch.custom-switch-lg .custom-control-label::after {
width: calc(2rem - 4px);
height: calc(2rem - 4px);
border-radius: calc(3rem - (2rem / 2));
}
.custom-switch.custom-switch-lg .custom-control-input:checked ~ .custom-control-label::after {
transform: translateX(calc(2rem - 0.25rem));
}
/* for xl */
.custom-switch.custom-switch-xl .custom-control-label {
padding-left: 4rem;
padding-bottom: 2.5rem;
}
.custom-switch.custom-switch-xl .custom-control-label::before {
height: 2.5rem;
width: calc(4rem + 0.75rem);
border-radius: 5rem;
}
.custom-switch.custom-switch-xl .custom-control-label::after {
width: calc(2.5rem - 4px);
height: calc(2.5rem - 4px);
border-radius: calc(4rem - (2.5rem / 2));
}
.custom-switch.custom-switch-xl .custom-control-input:checked ~ .custom-control-label::after {
transform: translateX(calc(2.5rem - 0.25rem));
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<div class="custom-control custom-switch custom-switch-sm">
<input type="checkbox" class="custom-control-input" id="customSwitch1">
<label class="custom-control-label" for="customSwitch1">Default Unchcked Switch</label>
</div>
<div class="custom-control custom-switch custom-switch-sm">
<input type="checkbox" class="custom-control-input" id="customSwitch2" checked>
<label class="custom-control-label" for="customSwitch2">Default Checked Switch</label>
</div>
<div class="custom-control custom-switch custom-switch-md">
<input type="checkbox" class="custom-control-input" id="customSwitch3">
<label class="custom-control-label" for="customSwitch3">Default Unchcked Switch</label>
</div>
<div class="custom-control custom-switch custom-switch-md">
<input type="checkbox" class="custom-control-input" id="customSwitch4" checked>
<label class="custom-control-label" for="customSwitch4">Default Checked Switch</label>
</div>
<div class="custom-control custom-switch custom-switch-lg">
<input type="checkbox" class="custom-control-input" id="customSwitch5">
<label class="custom-control-label" for="customSwitch5">Default Unchcked Switch</label>
</div>
<div class="custom-control custom-switch custom-switch-lg">
<input type="checkbox" class="custom-control-input" id="customSwitch6" checked>
<label class="custom-control-label" for="customSwitch6">Default Checked Switch</label>
</div>
<div class="custom-control custom-switch custom-switch-xl">
<input type="checkbox" class="custom-control-input" id="customSwitch7">
<label class="custom-control-label" for="customSwitch7">Default Unchcked Switch</label>
</div>
<div class="custom-control custom-switch custom-switch-xl">
<input type="checkbox" class="custom-control-input" id="customSwitch8" checked>
<label class="custom-control-label" for="customSwitch8">Default Checked Switch</label>
</div>
For Bootstrap 5
form-switch-sm, form-switch-md, form-switch-lg, form-switch-xl
SCSS: https://codepen.io/nisharg/pen/gOPLOYY
CSS: https://codepen.io/nisharg/pen/ExPNxYE
LIVE SNIPPET (CSS)
.form-check-input {
clear: left;
}
.form-switch.form-switch-sm {
margin-bottom: 0.5rem; /* JUST FOR STYLING PURPOSE */
}
.form-switch.form-switch-sm .form-check-input {
height: 1rem;
width: calc(1rem + 0.75rem);
border-radius: 2rem;
}
.form-switch.form-switch-md {
margin-bottom: 1rem; /* JUST FOR STYLING PURPOSE */
}
.form-switch.form-switch-md .form-check-input {
height: 1.5rem;
width: calc(2rem + 0.75rem);
border-radius: 3rem;
}
.form-switch.form-switch-lg {
margin-bottom: 1.5rem; /* JUST FOR STYLING PURPOSE */
}
.form-switch.form-switch-lg .form-check-input {
height: 2rem;
width: calc(3rem + 0.75rem);
border-radius: 4rem;
}
.form-switch.form-switch-xl {
margin-bottom: 2rem; /* JUST FOR STYLING PURPOSE */
}
.form-switch.form-switch-xl .form-check-input {
height: 2.5rem;
width: calc(4rem + 0.75rem);
border-radius: 5rem;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.0-alpha1/css/bootstrap.min.css">
<div class="form-check form-switch form-switch-sm">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckDefault">
<label class="form-check-label" for="flexSwitchCheckDefault">Default switch checkbox input</label>
</div>
<div class="form-check form-switch form-switch-sm">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked" checked>
<label class="form-check-label" for="flexSwitchCheckChecked">Checked switch checkbox input</label>
</div>
<div class="form-check form-switch form-switch-md">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckDefault">
<label class="form-check-label" for="flexSwitchCheckDefault">Default switch checkbox input</label>
</div>
<div class="form-check form-switch form-switch-md">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked" checked>
<label class="form-check-label" for="flexSwitchCheckChecked">Checked switch checkbox input</label>
</div>
<div class="form-check form-switch form-switch-lg">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckDefault">
<label class="form-check-label" for="flexSwitchCheckDefault">Default switch checkbox input</label>
</div>
<div class="form-check form-switch form-switch-lg">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked" checked>
<label class="form-check-label" for="flexSwitchCheckChecked">Checked switch checkbox input</label>
</div>
<div class="form-check form-switch form-switch-xl">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckDefault">
<label class="form-check-label" for="flexSwitchCheckDefault">Default switch checkbox input</label>
</div>
<div class="form-check form-switch form-switch-xl">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked" checked>
<label class="form-check-label" for="flexSwitchCheckChecked">Checked switch checkbox input</label>
</div>

FOR ALL BOOTSTRAP VERSIONS
No extra CSS/SCSS
Actually, the solution is quite simple, either you apply custom css/scss whatever to make the button bigger OR
You can use the transform: scale(); property of css on the switch to increase the size of the button, and trust me this is the easiest way to solve the problem and it works.
<div class="form-check form-switch py-5">
<!-- Size of the default switch will increase 1.8 times -->
<input class="form-check-input mx-2"
type="checkbox"
role="switch"
id="flexSwitchCheckDefault"
style="transform: scale(1.8);">
<label class="form-check-label"
for="flexSwitchCheckDefault"
id="AucTu">Autocomplete Turned Off</label>
</div>
PS. You dont need to add custom css/scss just dont forget to add some margin from the switch, or padding from the div (or somepart of it will get hidden) :)

In bootstrap 4, if you want to change or increase custom switch (a beautiful switch from iOS), you should dive into bootstrap's css file and change its dimensions of a class of custom-switch.
html code as following:
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="switchA" name="switch">
<label class="custom-control-label" for="switchA">Custom checkbox</label>
</div>
In boostrap.css (version 4.3.1 and not minified) file, you should find
the custom-switch class and change the following parameters in
the below. You must delete all the comments I added on.
.custom-switch {
padding-left: 2.25rem;
padding-bottom: 1rem; // added for positioning
}
.custom-control-label { // added for alignment with the switch
padding-top: 0.5rem;
padding-left: 2rem;
padding-bottom: 0.1rem;
}
.custom-switch .custom-control-label::before {
left: -2.25rem;
height: 2rem;
width: 3.5rem; // it was 1.75rem before. Sliding way is longer than before.
pointer-events: all;
border-radius: 1rem;
}
.custom-switch .custom-control-label::after {
top: calc(0.25rem + 2px);
left: calc(-2.25rem + 2px);
width: calc(2rem - 4px); // it was calc(1rem - 4px) before. Oval is bigger than before.
height: calc(2rem - 4px); // it was calc(1rem - 4px) before. Oval is bigger than before.
background-color: #adb5bd;
border-radius: 2rem; // it was 0.5rem before. Oval is bigger than before.
transition: background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out, -webkit-transform 0.15s ease-in-out;
transition: transform 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
transition: transform 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out, -webkit-transform 0.15s ease-in-out;
}
#media (prefers-reduced-motion: reduce) {
.custom-switch .custom-control-label::after {
transition: none;
}
}
.custom-switch .custom-control-input:checked ~ .custom-control-label::after {
background-color: #fff;
-webkit-transform: translateX(1.5rem); //translateX(0.75rem);
transform: translateX(1.5rem); //translateX(0.75rem);
}
After copied these code to Bootstrap file, you must delete all
comments I put in. Otherwise, It won't increase the size. I hope you enjoy
your a bigger pretty switch now.

It's probably best to have your own file and change the css variables for the dimensions of the switch:
#import "bootstrap/scss/_functions.scss";
#import "bootstrap/scss/_variables.scss";
$custom-control-indicator-size: 1.5rem;
$custom-switch-width: $custom-control-indicator-size * 1.75;
$custom-switch-indicator-border-radius: $custom-control-indicator-size / 2;
$custom-switch-indicator-size: subtract(
$custom-control-indicator-size,
$custom-control-indicator-border-width * 4
);
#import "bootstrap/scss/bootstrap";

It is not a good idea to change bootstrap components dimensions, you should change variables in bootstrap but it means to recompile library.
Anyway what you want can be done i this way:
.custom-control-label::before ,.custom-control-label::after{width:20px; height:20px}

This class .custom-switch-adaptive will make the switch adapt to text size and include the label text inside.
Demo
P.S. A similar feature will be added in Bootstrap v5
SCSS
.custom-switch.custom-switch-adaptive {
padding-left: 0;
.custom-control-label {
padding: 0 1.5em;
position: relative;
border-radius: 1em;
line-height: 1.4em;
color: $text-muted;
border: 1px solid $text-muted;
background-color: $light;
transition: background-color 200ms;
&::before {
content: none;
}
&::after {
position: absolute;
height: 1em;
top: 0.2em;
left: 0.2em;
width: 1em;
border-radius: 1em;
transition: left 200ms;
}
}
.custom-control-input:checked ~ .custom-control-label {
color: $white;
background-color: $success;
border-color: $success;
&::after {
background-color: $white;
left: calc(100% - 1.2em);
transform: none;
-webkit-transform: none;
}
}
}
HTML
<div class="form-group h1">
<div class="custom-control custom-switch custom-switch-adaptive">
<input id="test4" type="checkbox" class="custom-control-input">
<label for="test4" class="custom-control-label">h1 text size</label>
</div>
</div>

Bootstrap 5
I reworked the css from Nisharg Shah such that the Text is now nicely aligned with the switch for the Bootstrap V5 example.
Therefore, I took a look into the Bootstrap 5 css sources and increased all attributes that seem involved into sizing: padding-left, hight, margin-left, width and padding-top.
I think the result looks better and aligned with the default switch appearance style.
CSS and HTML:
.form-switch.form-switch-md {
padding-left: 4.5em;
height: 2.5em;
}
.form-switch.form-switch-md .form-check-input {
margin-left: -4.5em;
height: 2em;
width: 4em;
}
.form-switch.form-switch-md .form-check-label {
padding-top: 0.5em;
}
.form-switch.form-switch-lg {
padding-left: 8.5em;
height: 4.5em;
}
.form-switch.form-switch-lg .form-check-input {
margin-left: -8.5em;
height: 4em;
width: 8em;
}
.form-switch.form-switch-lg .form-check-label {
padding-top: 1.5em;
}
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckDefault">
<label class="form-check-label" for="flexSwitchCheckDefault">Original switch unchecked</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked" checked>
<label class="form-check-label" for="flexSwitchCheckChecked">Original switch checked</label>
</div>
<div class="form-check form-switch form-switch-md">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckDefault">
<label class="form-check-label" for="flexSwitchCheckDefault">Medium switch unchecked</label>
</div>
<div class="form-check form-switch form-switch-md">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked" checked>
<label class="form-check-label" for="flexSwitchCheckChecked">Medium switch checked</label>
</div>
<div class="form-check form-switch form-switch-lg">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckDefault">
<label class="form-check-label" for="flexSwitchCheckDefault">Large switch unchecked</label>
</div>
<div class="form-check form-switch form-switch-lg">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked" checked>
<label class="form-check-label" for="flexSwitchCheckChecked">Large switch checked</label>
</div>

Since you are trying to get it done in bootstrap, its easy.
Add a div container first with btn-group-toggle to use buttons in group (that is if you want.).
Add p-3 or p-5 class in label to expand it to your liking
<div class="btn-group-toggle" data-toggle="buttons">
<label class="btn btn-secondary p-3">
<input type="checkbox" id="customCheck" name="example1"> Check this custom checkbox
</label>
</div>

You can try the following way:
.custom-control-label::before,
.custom-control-label::after {
width: 2.25rem !important;
height: 2.25rem !important;
margin: 1rem;
}
.custom-control-label{
margin: 1.5rem 2rem;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck" name="example1">
<label class="custom-control-label" for="customCheck">Check this custom checkbox</label>
</div>

Here is a working example using custom css
.custom-checkbox-lg .custom-control-label::before,
.custom-checkbox-lg .custom-control-label::after {
width: 2rem;
height: 2rem;
}
.custom-checkbox-lg .custom-control-label {
margin: .5rem 1rem;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck1" name="example1">
<label class="custom-control-label" for="customCheck1">Check this custom checkbox</label>
</div>
<div class="custom-control custom-checkbox custom-checkbox-lg">
<input type="checkbox" class="custom-control-input" id="customCheck2" name="example1">
<label class="custom-control-label" for="customCheck2">Check this custom checkbox</label>
</div>

Found this while looking for a solution to the same issue but none of the previous answers worked well for me, especially when there was a label with the checkbox/switch.
Started playing around with margins and line heights but it seems that the solution might be much simpler than that - At least with Bootstrap V5, all sizes and margins are set relatively with "em", so changing the font size on the base form-check element resizes and aligns everything nicely, with or without the label.
EDIT: Just noticed the bottom margin is a bit off when there is no label, but it's easily fixable by adding an empty label or adjusting the margins/padding manually as needed.
.form-check-lg {
font-size: 150%;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"/>
<h3>default font size:</h3>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="checkbox_1">
<label class="form-check-label" for="checkbox_1">default</label>
</div>
<div class="form-check"><input class="form-check-input" type="checkbox" id="checkbox_2">
<label class="form-check-label" for="checkbox_2">default</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox">
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox">
</div>
<h3>larger font size:</h3>
<div class="form-check form-switch form-check-lg">
<input class="form-check-input" type="checkbox" id="checkbox_3">
<label class="form-check-label" for="checkbox_3">large</label>
</div>
<div class="form-check form-check-lg">
<input class="form-check-input" type="checkbox" id="checkbox_4">
<label class="form-check-label" for="checkbox_4">large</label>
</div>
<div class="form-check form-switch form-check-lg">
<input class="form-check-input" type="checkbox">
</div>
<div class="form-check form-check-lg">
<input class="form-check-input" type="checkbox">
</div>

This is my solution... Just used Bootstrap & FontAvesome....
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/js/all.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="ps-3">
<div class="h3">bootstrap#5.1.3 & font-awesome/6.2.0</div>
<div class="col-12">
<label>Label Top</label>
<span class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch">
<label class="form-check-label">Label Side</label>
</span>
</div>
<div class="col-12">
<label>Label Top</label>
<span class="form-check form-switch fa-2x">
<input class="form-check-input" type="checkbox" role="switch">
<label class="form-check-label">Label Side</label>
</span>
</div>
<div class="col-12">
<label>Label Top</label>
<span class="form-check form-switch fa-2x">
<input class="form-check-input" type="checkbox" role="switch">
<label class="form-check-label">Label Side</label>
</span>
</div>
<div class="col-12">
<label>Label Top</label>
<span class="form-check form-switch fa-3x">
<input class="form-check-input" type="checkbox" role="switch">
<label class="form-check-label">Label Side</label>
</span>
</div>
<div class="col-12">
<label>Label Top</label>
<span class="form-check form-switch fa-4x">
<input class="form-check-input" type="checkbox" role="switch">
<label class="form-check-label">Label Side</label>
</span>
</div>
</div>

Related

Changing accordion so only closes when header is clicked

I've got a simple few accordions that are made interactive with vanilla JS. They open perfectly, however once they're open if you click anywhere in the div then they close. I know this is happening because of the following line:
var allPanelElems = accordionElem.querySelectorAll(".checkout-accordion");
However, I'm scratching my head as to how to rephrase the JavaScript so they only close when you click on the accordion header, and not the accordion body too.
Here's the full code:
function initAccordion(accordionElem){
function handlePanelClick(event){
showPanel(event.currentTarget);
}
function showPanel(panel){
let expandedPanel = accordionElem.querySelector(".active");
if (panel === expandedPanel){
expandedPanel.classList.remove("active");
} else {
panel.classList.add("active");
}
}
var allPanelElems = accordionElem.querySelectorAll(".checkout-accordion");
for (var i = 0, len = allPanelElems.length; i < len; i++){
allPanelElems[i].addEventListener("click", handlePanelClick);
}
showPanel(allPanelElems[0])
}
initAccordion(document.getElementById("checkout-details-container"));
.checkout-details-section {
height: auto;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
#checkout-details-container {
height: 90%;
width: 90%;
display: flex;
align-items: center;
justify-content: space-around;
flex-direction: column;
margin: 50px 0;
}
.checkout-accordion {
height: auto;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
margin: 50px 0;
}
.accordion-header {
height: 10vh;
width: 100%;
display: flex;
align-items: center;
justify-content: space-around;
background-color: #f9f9f9;
cursor: pointer;
}
.accordion-dropdown {
height: auto;
width: 50%;
align-items: center;
justify-content: center;
flex-direction: column;
margin: 30px 0;
}
.accordion-dropdown label {
height: 40px;
width: 100%;
margin: 10px 0;
font-size: 24px;
font-weight: bold;
}
.accordion-dropdown input {
height: 40px;
width: 100%;
margin: 20px 0;
font-size: 22px;
color: #999;
background-color: #f9f9f9;
}
/* Start of checkout details accordion functionality */
#checkout-details-container .accordion-dropdown {
display: none;
}
#checkout-details-container .active .accordion-dropdown {
display: flex;
}
<!-- Checkout Details Section -->
<section class="checkout-details-section">
<div id="checkout-details-container">
<div class="checkout-accordion active"> <!-- Personal Details -->
<div class="accordion-header">
<h1>Personal Details</h1>
<i class="fas fa-chevron-down"></i>
</div>
<div class="accordion-dropdown">
<label for="fullName">Full name</label>
<input type="text" name="fullName" value="">
<label for="email">Enter your email</label>
<input type="email" name="email" value="">
<label for="phone">Enter your phone number</label>
<input type="tel" name="phone" value="">
</div>
</div>
<div class="checkout-accordion"> <!-- Shipping Address -->
<div class="accordion-header">
<h1>Shipping Address</h1>
<i class="fas fa-chevron-down"></i>
</div>
<div class="accordion-dropdown">
<label for="firstLine">House number and street name</label>
<input type="text" name="firstLine" value="">
<label for="secondLine">Enter your local area</label>
<input type="text" name="secondLine" value="">
<label for="city">Enter your city</label>
<input type="text" name="city" value="">
<label for="region">Enter your region</label>
<input type="text" name="region" value="">
<label for="postcode">Enter your postcode</label>
<input type="text" name="postcode" value="">
<label for="country">Enter your country</label>
<input type="text" name="country" value="">
</div>
</div>
<div class="checkout-accordion"> <!-- Billing Address -->
<div class="accordion-header">
<h1>Billing Address</h1>
<i class="fas fa-chevron-down"></i>
</div>
<div class="accordion-dropdown">
<label for="billingFirst">House number and street name</label>
<input type="text" name="billingFirst" value="">
<label for="billingSecond">Enter your local area</label>
<input type="text" name="billingSecond" value="">
<label for="billingCity">Enter your city</label>
<input type="text" name="billingCity" value="">
<label for="billingRegion">Enter your region</label>
<input type="text" name="billingRegion" value="">
<label for="billingPostcode">Enter your postcode</label>
<input type="text" name="billingPostcode" value="">
<label for="billingCountry">Enter your country</label>
<input type="text" name="billingCountry" value="">
</div>
</div>
<div class="checkout-accordion"> <!-- Payment Details -->
<div class="accordion-header">
<h1>Payment Details</h1>
<i class="fas fa-chevron-down"></i>
</div>
<div class="accordion-dropdown">
<label for="cardNumber">Enter your card number</label>
<input type="text" name="cardNumber" value="">
<label for="expiryDate">Enter your card's expiry date</label>
<input type="date" name="expiryDate" placeholder="DD / YYYY" required pattern="[0-9]{4}-[0-9]{2}" value="">
<label for="csv">Enter your card's CSV</label>
<input type="text" name="csv" placeholder="123" require pattern="[0-9]{4}" value="">
</div>
</div>
</div>
</section>
Weirdly too, it only happens on the second click on the accordion, not the first. There's another weird bug which means if two accordions are open, the first accordion won't close on click until you close the second. If anyone has any idea why that's happening, an answer would be great too.
Thanks, hopefully my questions clear. 😊
You could conditionally handle the panel click event by checking if it's coming from the header (as you want). Your JavaScript should be looking somewhat like:
function initAccordion(accordionElem) {
function handlePanelClick(event) {
// if the event is coming from the header then show panel
if (event.path[0].classList[0] === "accordion-header"){
showPanel(event.currentTarget);
}
}
function showPanel(panel) {
let expandedPanel = accordionElem.querySelector(".active");
if (panel === expandedPanel) {
expandedPanel.classList.remove("active");
} else {
panel.classList.add("active");
}
}
var allPanelElems = accordionElem.querySelectorAll(".checkout-accordion");
for (var i = 0, len = allPanelElems.length; i < len; i++) {
allPanelElems[i].addEventListener("click", handlePanelClick);
}
showPanel(allPanelElems[0]);
}
initAccordion(document.getElementById("checkout-details-container"));
.checkout-details-section {
height: auto;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
#checkout-details-container {
height: 90%;
width: 90%;
display: flex;
align-items: center;
justify-content: space-around;
flex-direction: column;
margin: 50px 0;
}
.checkout-accordion {
height: auto;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
margin: 50px 0;
}
.accordion-header {
height: 10vh;
width: 100%;
display: flex;
align-items: center;
justify-content: space-around;
background-color: #f9f9f9;
cursor: pointer;
}
.accordion-dropdown {
height: auto;
width: 50%;
align-items: center;
justify-content: center;
flex-direction: column;
margin: 30px 0;
}
.accordion-dropdown label {
height: 40px;
width: 100%;
margin: 10px 0;
font-size: 24px;
font-weight: bold;
}
.accordion-dropdown input {
height: 40px;
width: 100%;
margin: 20px 0;
font-size: 22px;
color: #999;
background-color: #f9f9f9;
}
/* Start of checkout details accordion functionality */
#checkout-details-container .accordion-dropdown {
display: none;
}
#checkout-details-container .active .accordion-dropdown {
display: flex;
}
<!-- Checkout Details Section -->
<section class="checkout-details-section">
<div id="checkout-details-container">
<div class="checkout-accordion active"> <!-- Personal Details -->
<div class="accordion-header">
<h1>Personal Details</h1>
<i class="fas fa-chevron-down"></i>
</div>
<div class="accordion-dropdown">
<label for="fullName">Full name</label>
<input type="text" name="fullName" value="">
<label for="email">Enter your email</label>
<input type="email" name="email" value="">
<label for="phone">Enter your phone number</label>
<input type="tel" name="phone" value="">
</div>
</div>
<div class="checkout-accordion"> <!-- Shipping Address -->
<div class="accordion-header">
<h1>Shipping Address</h1>
<i class="fas fa-chevron-down"></i>
</div>
<div class="accordion-dropdown">
<label for="firstLine">House number and street name</label>
<input type="text" name="firstLine" value="">
<label for="secondLine">Enter your local area</label>
<input type="text" name="secondLine" value="">
<label for="city">Enter your city</label>
<input type="text" name="city" value="">
<label for="region">Enter your region</label>
<input type="text" name="region" value="">
<label for="postcode">Enter your postcode</label>
<input type="text" name="postcode" value="">
<label for="country">Enter your country</label>
<input type="text" name="country" value="">
</div>
</div>
<div class="checkout-accordion"> <!-- Billing Address -->
<div class="accordion-header">
<h1>Billing Address</h1>
<i class="fas fa-chevron-down"></i>
</div>
<div class="accordion-dropdown">
<label for="billingFirst">House number and street name</label>
<input type="text" name="billingFirst" value="">
<label for="billingSecond">Enter your local area</label>
<input type="text" name="billingSecond" value="">
<label for="billingCity">Enter your city</label>
<input type="text" name="billingCity" value="">
<label for="billingRegion">Enter your region</label>
<input type="text" name="billingRegion" value="">
<label for="billingPostcode">Enter your postcode</label>
<input type="text" name="billingPostcode" value="">
<label for="billingCountry">Enter your country</label>
<input type="text" name="billingCountry" value="">
</div>
</div>
<div class="checkout-accordion"> <!-- Payment Details -->
<div class="accordion-header">
<h1>Payment Details</h1>
<i class="fas fa-chevron-down"></i>
</div>
<div class="accordion-dropdown">
<label for="cardNumber">Enter your card number</label>
<input type="text" name="cardNumber" value="">
<label for="expiryDate">Enter your card's expiry date</label>
<input type="date" name="expiryDate" placeholder="DD / YYYY" required pattern="[0-9]{4}-[0-9]{2}" value="">
<label for="csv">Enter your card's CSV</label>
<input type="text" name="csv" placeholder="123" require pattern="[0-9]{4}" value="">
</div>
</div>
</div>
</section>

how to remove # in url when modal popup and submitted

Got a problem here.
I have a modal and when i open it using my code, my url extension extends like having this #mymodalid .
$(document).on("click", "input[name='txt1']", function() {
$('#modal1').show();
});
$(document).on("click", "input[name='txt2']", function() {
$('#modal2').show();
});
$(document).on("click", ".btn1", function() {
if ($('#modal1 :checkbox:checked').length > 1) {
$('input[name=txt1]').val('multiple');
} else {
$('input[name=txt1]').val($('#modal1 :checkbox:checked').prev('label').text());
}
$('#modal1').hide();
});
$(document).on("click", ".btn2", function() {
$('#modal2').hide();
});
$(".radio").change(function() {
$(".radio").prop('checked', false);
$(this).prop('checked', true);
});
/* modal layout */
.modalwrapper {
top: 0;
left: 0;
opacity: 0;
position: absolute;
visibility: hidden;
box-shadow: 0 3px 7px rgba(0,0,0,.25);
box-sizing: border-box;
transition: all .4s ease-in-out;
-webkit-transition: all .4s ease-in-out;
-moz-transition: all .4s ease-in-out;
}
.modalwrapper:target {
opacity: 1;
visibility: visible
}
.overlay {
background-color: #000;
background: rgba(0,0,0,.8);
height: 100%;
left: 0;
position: fixed;
top: 0;
width: 100%;
z-index: 1;
}
.modalcontainer {
display: table;
background-color: #777;
position: relative;
z-index: 100;
color: #fff;
padding: 5px;
}
.modalcol1 { display: table-cell; }
.clear { clear: both; }
.modalwrapper input[type=checkbox] {
float: right;
margin-right: 20px;
}
.savebutton input[type=submit] {
float: right;
background-color: maroon;
color: #fff;
border: none;
padding: 5px 10px;
margin-top: 5px;
margin-right: 20px;
}
/* modal layout ends here */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" name="testform" action="">
<a href="#modal1">
<!-- when the input textbox was clicked, modal will pop up -->
Label 1
<input readonly type="text" name="txt1" placeholder="inputTest">
</a>
<br>
<br>
<a href="#modal2">
<!-- when the input textbox was clicked, modal will pop up -->
Label 2
<input readonly type="text" name="txt2" placeholder="inputTest">
</a>
<!-- modal starts here -->
<div class="modalwrapper" id="modal1">
<!-- modal -->
<div class="modalcontainer">
<div class="modalcol1">
<label>Test 1</label>
<input type="checkbox" name="test_modal[]" value="1">
<div class="clear"></div>
<label>Test 2</label>
<input type="checkbox" name="test_modal[]" value="2">
<div class="clear"></div>
<label>Test 3</label>
<input type="checkbox" name="test_modal[]" value="3">
<div class="clear"></div>
<label>Test 4</label>
<input type="checkbox" name="test_modal[]" value="4">
<div class="clear"></div>
<label>Test 5</label>
<input type="checkbox" name="test_modal[]" value="5">
<div class="clear"></div>
<div class="savebutton">
<button class="btn1" type="button" value="Submit">Submit</button>
</div>
</div>
<!-- close modalcol1 -->
</div>
<!-- close modalcontainer -->
<div class="overlay"></div>
</div>
<!-- close modalwrapper -->
<div class="modalwrapper" id="modal2">
<!-- modal -->
<div class="modalcontainer">
<div class="modalcol1">
<label>Mango</label>
<input class="radio" type="checkbox" name="fruit1" value="1">
<div class="clear"></div>
<label>Banna</label>
<input class="radio" type="checkbox" name="fruit2" value="2">
<div class="clear"></div>
<label>Grapes</label>
<input class="radio" type="checkbox" name="fruit3" value="3">
<div class="clear"></div>
<div class="savebutton">
<button class="btn2" type="button" value="Submit">Submit</button>
</div>
</div>
<!-- close modalcol1 -->
</div>
<!-- close modalcontainer -->
<div class="overlay"></div>
</div>
<!-- close modalwrapper -->
</form>
What I need to do is that:
Show the modal as it needs on my program.
Checked some checkbox
Close the modal but on the url, #mymodalid should be remove once close.
Even if i closed the modal, what i was checked should be retain
I need to remove #mymodalid because when i redirected to the page itself upon submission, the modal will popup because of that #
Anyone, please help me.
Add a callback to your hide() functions:
$('#modal1').hide(400, removeHashFromUrl());
function removeHashFromUrl()
{
window.location.hash = '';
}
Or you could just just inline the callback, but it's obviously reusable in a function:
$('#modal1').hide(400, function(){window.location.hash = '';});
PS: 400 is the default speed for jQuery's hide().

$(document).on("change") not fire up on Mozilla Firefox

I have an issue with radio button and jquery. The onchange is working in Chrome but is not working in Mozilla Firefox. I put a breakpoint on the line below on Chrome and the browser is debugging normally, but when I am on Mozilla, this onchange never fires up
$(document).on("change", 'input[type="radio"]', function () {//code for doing something}
My HTML code is
<div id="tab-Pcy-2041" class="tab-pane active">
<div class="radio radio-small">
<label class="">
<div class="i-radio checked">
<input class="i-radio" data-ano="1" data-chno="0"
data-adce="0" data-chce="0" data-for="rbtncy"
xname="rbcy204" name="rbtncy2040" value="22038"
checked="checked" style="position: absolute; opacity: 0;" type="radio" />
<ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%;
display: block; width: 100%; height: 100%; margin: 0px; padding: 0px;
background: rgb(255, 255, 255) none repeat scroll 0% 0%; border:
0px none; opacity: 0;">
</ins>
</div>
test
</label>
</div>
<div class="radio radio-small">
<label class="">
<div class="i-radio">
<input class="i-radio" data-ano="1" data-chno="0"
data-ace="15" data-chce="15"
data-for="rbtncy" xname="rbtncy204"
name="rbtncy2040" value="22039"
style="position: absolute; opacity: 0;" type="radio" />
<ins class="iCheck-helper" style="position:
absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%;
margin: 0px; padding: 0px; background: rgb(255, 255, 255)
none repeat scroll 0% 0%; border: 0px none; opacity: 0;">
</ins>
</div>
test33:
<span class="ItemtestClass" data-itemce="15">
<span class="CurrencySymbolClass">€</span>
<span class="Itce"> 15</span>
</span>
Cd:
<span class="ItemtestClass" data-itemce="15">
<span class="CurrencySymbolClass">€</span>
<span class="Itemce"> 15</span>
</span>
</label>
</div>
</div>
With minimal markup and script you can refer to the working snippet below
$(document).on("change","input[type=radio]", function(){
console.log($(this).prop("id"));
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="rd1" id="option1" /><label for="option1">Option 1</label><br />
<input type="radio" name="rd1" id="option2" /><label for="option2">Option 2</label><br />
<input type="radio" name="rd1" id="option3" /><label for="option3">Option 3</label><br />

How to make a radio appear as the header? Also how do I get the radio to decide the next box?

I have been struggeling quite a while trying to figure out how to get the radio selected to show up in the header. Example: Let's say I click Baden-Wurttemberg, how can I get it to say Baden-Wurttemberg where it currently says province?
Also, in other to advance further, when you click Baden-Wurttemberg, it would be desirable to get the regions within that bundesland (province) to come up as the next option as you move on to the next box. Example: If you want to find Munich, you should be able to click on Bavaria and move on to the next box having Munich as an option.
I would appreciate it a lot if anyone could share their knowlegde!
label {
display: inline-block;
color: black;
padding: 5px;
}
input[type="radio"] {
vertical-align: bottom;
}
.menu-container {
width:100%;
margin: 0 auto;
padding: 0 0;
}
.menu {
width:100%;
font-family: Verdana, Geneva, sans-serif;
font-weight: 400;
font-size: 15px;
line-height: 15px;
position: relative;
padding: 0 0 0 4px;
margin: 0;
background-color: white;
}
.menu a, .menu a:link, .menu a:visited, .menu a:focus, span {
color: #23255e;
text-decoration: none;
}
.menu a:hover {
color: #23255e;
text-decoration: none;
}
.menu > li {
display: inline-block;
text-align: center;
margin-left: -15px;
border-left: 0px solid rgba(255, 255, 255, 0.11);
box-shadow: -0px 0 0 rgba(0, 0, 0, 0.1);
}
.menu > li > a {
padding:20px 140px;
display: block;
}
.menu > li:hover > a {
color: white;
}
.menu > li:hover {
background-color: #23255e;
}
/* Megadrop width dropdown */
.menu > li > .megadrop {
opacity: 0;
visibility: hidden;
position: absolute;
list-style: none;
top:53px;
left: 0px;
width: 70%;
min-height: 100px;
text-align: left;
margin-top:30px;
padding-left: 50px;
padding-top: 5px;
padding-bottom: 10px;
z-index: 99;
overflow: hidden;
border-left: 1px solid #23255e;
border-right: 1px solid #23255e;
border-bottom: 1px solid #23255e;
}
.menu > li:hover .megadrop {
opacity: 1;
visibility: visible;
margin-top: 0px;
}
.menu ul li:hover:after {
color: #23255e;
}
.menu .col {
width: 14.1%;
float: left;
color:white;
margin: 0 0 0 ;
}
.menu .col ul {
padding: 0;
margin: 0;
}
.menu .col ul li {
padding: 0;
list-style: none;
font-size: 11px;
}
.menu .col h3 {
font-size: 16px;
padding: 10px 0;
font-weight: bold;
margin: 5px 0 5px 0;
color: #23255e;
background: white;
}
.menu .col ul li a {
display: block;
padding: 0 0 15px 0;
color: #23255e;
}
.menu .col ul li a:hover {
color: #111;
text-decoration: none;
}
.menu > li > ul li ul, .menu li >ul li, .menu > li > .megadrop, .menu > li > ul, .menu > li {
transition: all 0.2s ease-in-out;
-moz-transition: all 0.2s ease-in-out;
-webkit-transition: all 0.2s ease-in-out;
-ms-transition: all 0.2s ease-in-out;
-o-transition: all 0.2s ease-in-outs;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="euprop.css">
</head>
<body>
<div>
<ul class="menu">
<li class="dropdown">Province
<div class="megadrop">
<div class="col">
<h3>Independent Cities</h3>
<ul>
<li><label for="radio_1"><input type="radio" name="radio" id="radio_1" />Berlin</label>
</li>
<li><label for="radio_2"><input type="radio" name="radio" id="radio_2" />Bremen</label>
</li>
<li><label for="radio_3"><input type="radio" name="radio" id="radio_3" />Hamburg</label>
</li>
</ul>
</div>
<div class="col">
<h3>Provinces</h3>
<ul>
<li><label for="radio_4"><input type="radio" name="radio" id="radio_4" />Baden-Wurttemberg</label>
</li>
<li><label for="radio_5"><input type="radio" name="radio" id="radio_5" />Bavaria</label>
</li>
<li><label for="radio_6"><input type="radio" name="radio" id="radio_6" />Hesse</label>
</li>
<li><label for="radio_7"><input type="radio" name="radio" id="radio_7" />Mecklenburg</label>
</li>
<li><label for="radio_8"><input type="radio" name="radio" id="radio_8" />Lower Saxony</label>
</li>
<li><label for="radio_9"><input type="radio" name="radio" id="radio_9" />North Rhine-Westphalia</label>
</li>
</ul>
</div>
<div class="col">
<h3>Title</h3>
<ul>
<li><label for="radio_10"><input type="radio" name="radio" id="radio_10" />Rhineland-Palatinate</label>
</li>
<li><label for="radio_11"><input type="radio" name="radio" id="radio_11" />Saarland</label>
</li>
<li><label for="radio_12"><input type="radio" name="radio" id="radio_12" />Saxony</label>
</li>
<li><label for="radio_13"><input type="radio" name="radio" id="radio_13" />Saxony-Anhalt</label>
</li>
<li><label for="radio_14"><input type="radio" name="radio" id="radio_14" />Schleswig-Holstein</label>
</li>
<li><label for="radio_15"><input type="radio" name="radio" id="radio_15" />Thuringia</label>
</li>
</ul>
</div>
</div>
</li>
<li class="dropdown"> Region
<div class="megadrop">
<div class="col">
<h3>Independent Cities</h3>
<ul>
<li><label for="radio_16"><input type="radio" name="radio" id="radio_16" />Baden-Baden</label>
</li>
<li><label for="radio_17"><input type="radio" name="radio" id="radio_17" />Freiburg</label>
</li>
<li><label for="radio_18"><input type="radio" name="radio" id="radio_18" />Heidelberg</label>
</li>
<li><label for="radio_19"><input type="radio" name="radio" id="radio_19" />Heilbronn</label>
</li>
<li><label for="radio_20"><input type="radio" name="radio" id="radio_20" />Karlsruhe</label>
</li>
<li><label for="radio_21"><input type="radio" name="radio" id="radio_21" />Mannheim</label>
</li>
</ul>
</div>
<div class="col">
<h3>Independent Cities</h3>
<ul>
<li><label for="radio_22"><input type="radio" name="radio" id="radio_22" />Pforzheim</label>
</li>
<li><label for="radio_23"><input type="radio" name="radio" id="radio_23" />Stuttgart</label>
</li>
<li><label for="radio_24"><input type="radio" name="radio" id="radio_24" />Ulm</label>
</ul>
</div>
<div class="col">
<h3>Regions</h3>
<ul>
<li><label for="radio_4"><input type="radio" name="radio" id="radio_4" />Freiburg</label>
</li>
<li><label for="radio_5"><input type="radio" name="radio" id="radio_5" />Karlsruhe</label>
</li>
<li><label for="radio_6"><input type="radio" name="radio" id="radio_6" />Stuttgart</label>
</li>
<li><label for="radio_7"><input type="radio" name="radio" id="radio_7" />Tubingen</label>
</li>
</ul>
</div>
</div>
</li>
<li class="dropdown">District
<div class="megadrop">
<div class="col">
<h3>Independent Cities</h3>
<ul>
<li><label for="radio_1"><input type="radio" name="radio" id="radio_1" />Berlin</label>
</li>
<li><label for="radio_2"><input type="radio" name="radio" id="radio_2" />Bremen</label>
</li>
<li><label for="radio_3"><input type="radio" name="radio" id="radio_3" />Hamburg</label>
</li>
</ul>
</div>
<div class="col">
<h3>Provinces</h3>
<ul>
<li><label for="radio_4"><input type="radio" name="radio" id="radio_4" />Baden-Wurttemberg</label>
</li>
<li><label for="radio_5"><input type="radio" name="radio" id="radio_5" />Bavaria</label>
</li>
<li><label for="radio_6"><input type="radio" name="radio" id="radio_6" />Hesse</label>
</li>
<li><label for="radio_7"><input type="radio" name="radio" id="radio_7" />Mecklenburg</label>
</li>
<li><label for="radio_8"><input type="radio" name="radio" id="radio_8" />Lower Saxony</label>
</li>
<li><label for="radio_9"><input type="radio" name="radio" id="radio_9" />North Rhine-Westphalia</label>
</li>
</ul>
</div>
<div class="col">
<h3>Title</h3>
<ul>
<li><label for="radio_10"><input type="radio" name="radio" id="radio_10" />Rhineland-Palatinate</label>
</li>
<li><label for="radio_11"><input type="radio" name="radio" id="radio_11" />Saarland</label>
</li>
<li><label for="radio_12"><input type="radio" name="radio" id="radio_12" />Saxony</label>
</li>
<li><label for="radio_13"><input type="radio" name="radio" id="radio_13" />Saxony-Anhalt</label>
</li>
<li><label for="radio_14"><input type="radio" name="radio" id="radio_14" />Schleswig-Holstein</label>
</li>
<li><label for="radio_15"><input type="radio" name="radio" id="radio_15" />Thuringia</label>
</li>
</ul>
</div>
</div>
</li>
<li class="dropdown">Municapality
<div class="megadrop">
<div class="col">
<h3>Title</h3>
<ul>
<li>Sub-menu 1
</li>
<li>Sub-menu 2
</li>
<li>Sub-menu 3
</li>
</ul>
</div>
<div class="col">
<h3>Title</h3>
<ul>
<li>Sub-menu 1
</li>
<li>Sub-menu 2
</li>
<li>Sub-menu 3
</li>
</ul>
</div>
<div class="col">
<h3>Title</h3>
<ul>
<li>Sub-menu 1
</li>
<li>Sub-menu 2
</li>
<li>Sub-menu 3
</li>
</ul>
</div>
<div class="col">
<h3>Title</h3>
<ul>
<li>Sub-menu 1
</li>
<li>Sub-menu 2
</li>
<li>Sub-menu 3
</li>
</ul>
</div>
</div>
</li>
</li>
</ul>
</div>
</body>
</html>
I am no expert but i would say you may need to add a "onclick=" to the input and a function that maybe uses a .innerHTML to push the value from the radio button into the heading, your heading will need an id also like: <h3 id="provincesAnswer">Provinces</h3>
The function will need to take into account that all the radios are point at the heading, so you may need to go inline (i am sure someone else would have an easier way but as i said i am no expert).
So the radio may look like:
<input type="radio" name="radio" id="radio_4" onclick="check(this.value)" value="Baden-Wurttemberg">Baden-Wurttemberg
So to simplify what i mean
here is the script:
<script>
function check(provincesValue) {
document.getElementById("provincesAnswer").value=provincesValue;
}
</script>
And Your Heading:
<h3 type="text" id="provincesAnswer">Provinces</h3>
And A Few Radio buttons for example:
<li><label for="radio_4"><input type="radio" name="provincesValue" id="radio_4" onclick="check(this.value)" value="Baden-Wurttemberg" />Baden-Wurttemberg</label>
</li>
<li><label for="radio_5"><input type="radio" name="provincesValue" id="radio_5" onclick="check(this.value)" value="Bavaria" />Bavaria</label>
</li>
Hope this helps

Clear checkbox button not working

I have a CLEAR button. The aim of that CLEAR button is that when a user clicks the button, all selected checkbox values are removed. Thats it. Now somehow I managed to get a checkbox with the CLEAR button (seemed for me the only way), and also upon clicking then all checkbox values are selected and when clicking again all checkbox values are removed. Is it possible to get only the button and then just one click for just removal of selected checkboxes? My current code only works for the first filter-menu BRAND, but for the others it does not work, I cant figure out why.
Here my BOOTPLY - BOOTPLY
$("#clear").change(function () {
$("input:checkbox").prop('checked', $(this).prop("checked"));
});
/* CSS used here will be applied after bootstrap.css */
/* FILTER_datafeed */
.scrollable-menu {
height: auto;
max-height: 200px;
overflow-x: hidden;
}
/* Filter-menu APPLY and CLEAR */
.select_filter > .multi_select_container > .div_form > .btn_apply {
border-right: 1px solid #e6e6e6;
box-sizing: border-box;
display: inline-block;
float: left;
padding: 10px 16px 10px 17px;
text-align: center;
width: 50%;
}
.select_filter > .multi_select_container > .div_form > .btn_clear.disabled, .select_filter > .multi_select_container > .div_form > .btn_clear[disabled], .select_filter > .multi_select_container > .div_form > .btn_clear[disabled]:hover {
background-color: transparent;
color: #ccc;
}
.select_filter > .multi_select_container > .div_form > .btn_clear {
box-sizing: border-box;
display: inline-block;
float: right;
padding: 10px 17px;
text-align: center;
width: 50%;
}
.checkbox :hover {
background-color:red;
cursor:pointer;
width:100%;
}
.div_form :hover {
background-color:green;
cursor:pointer;
}
.btn_clear {
float: right;
display: inline-block;
box-sizing: border-box;
width: 50%;
padding: 10px 17px;
text-align: center;
}
.btn_apply {
float: left;
display: inline-block;
box-sizing: border-box;
width: 50%;
padding: 10px 17px;
text-align: center;
}
/* Type-ahead plugin */
.twitter-typeahead .tt-query,
.twitter-typeahead .tt-hint {
margin-bottom: 0;
}
.tt-dropdown-menu {
min-width: 160px;
margin-top: 2px;
padding: 5px 0;
background-color: #fff;
border: 1px solid #ccc;
border: 1px solid rgba(0,0,0,.2);
*border-right-width: 2px;
*border-bottom-width: 2px;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
-webkit-box-shadow: 0 5px 10px rgba(0,0,0,.2);
-moz-box-shadow: 0 5px 10px rgba(0,0,0,.2);
box-shadow: 0 5px 10px rgba(0,0,0,.2);
-webkit-background-clip: padding-box;
-moz-background-clip: padding;
background-clip: padding-box;
}
.tt-suggestion {
display: block;
padding: 3px 20px;
}
.tt-suggestion.tt-is-under-cursor {
color: #fff;
background-color: #0081c2;
background-image: -moz-linear-gradient(top, #0088cc, #0077b3);
background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#0088cc), to(#0077b3));
background-image: -webkit-linear-gradient(top, #0088cc, #0077b3);
background-image: -o-linear-gradient(top, #0088cc, #0077b3);
background-image: linear-gradient(to bottom, #0088cc, #0077b3);
background-repeat: repeat-x;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff0088cc', endColorstr='#ff0077b3', GradientType=0)
}
.tt-suggestion.tt-is-under-cursor a {
color: #fff;
}
.tt-suggestion p {
margin: 0;
}
/* Type-ahead plugin END */
<div class="btn-toolbar">
<!--Default buttons with dropdown menu-->
<div class="btn-group">
<button class="btn btn-default" type="button">Brand</button>
<button class="btn btn-default dropdown-toggle" data-toggle="dropdown" type="button"><span class="caret"></span></button>
<div class="dropdown-menu scrollable-menu" style="margin-left: 2em">
<input class="typeahead" placeholder="Search values" type="text">
<div class="checkbox">
<label><input value="" type="checkbox"> Alpha</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Beta
</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Gamma</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Delta</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Omega</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Koppa
</label>
</div>
<div class="div_form">
<span class="btn_apply" id="apply">Apply</span>
<span class="btn_clear"><input id="clear" type="checkbox">Clear</span>
</div>
</div>
</div><!--Primary buttons with dropdown menu-->
<div class="btn-group">
<button class="btn btn-primary" type="button">Colour</button>
<button class="btn btn-primary dropdown-toggle" data-toggle="dropdown" type="button"><span class="caret"></span></button>
<div class="dropdown-menu scrollable-menu" style="margin-left: 2em">
<input class="typeahead" placeholder="Search values" type="text">
<div class="checkbox">
<label><input value="" type="checkbox"> Eins</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Zwei
</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Drei</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Vier</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Fünf</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Sechs
</label>
</div>
<div class="div_form">
<span class="btn_apply" id="apply">Apply</span>
<span class="btn_clear"><input id="clear" type="checkbox">Clear</span>
</div>
</div>
</div><!--Info buttons with dropdown menu-->
<div class="btn-group">
<button class="btn btn-info" type="button">Merchant</button>
<button class="btn btn-info dropdown-toggle" data-toggle="dropdown" type="button"><span class="caret"></span></button>
<div class="dropdown-menu scrollable-menu" style="margin-left: 2em">
<input class="typeahead" placeholder="Search values" type="text">
<div class="checkbox">
<label><input value="" type="checkbox"> First value</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Second option
</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Third choice</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Fourth alternative</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Fifth decision</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Sixt focus
</label>
</div>
<div class="div_form">
<span class="btn_apply" id="apply">Apply</span>
<span class="btn_clear"><input id="clear" type="checkbox">Clear</span>
</div>
</div>
</div><!--Success buttons with dropdown menu-->
<div class="btn-group">
<button class="btn btn-danger" type="button">Price</button>
<button class="btn btn-danger dropdown-toggle" data-toggle="dropdown" type="button"><span class="caret"></span></button>
<div class="dropdown-menu scrollable-menu" style="margin-left: 2em">
<input class="typeahead" placeholder="Search values" type="text">
<div class="checkbox">
<label><input value="" type="checkbox"> Value-1</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Value-2
</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Value-3</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Value-4</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Value-5</label>
</div>
<div class="checkbox">
<label><input value="" type="checkbox"> Value-6
</label>
</div>
<div class="div_form">
<span class="btn_apply" id="apply">Apply</span>
<span class="btn_clear"><input id="clear" type="checkbox">Clear</span>
</div>
</div>
</div>
</div><!--Success buttons with dropdown menu-->
Man you are using the same ID four times. The ID selector always return a single element, in your case you are only attaching the event to the first input with the id #clear so it will be working ONLY on the first clear button. Use a class instead and somehow scope the changes to affect only the related checkboxes.
Change the IDs to classes and use the following:
$(".clear").change(function () {
$(this).closest('.dropdown-menu').find('input:checkbox').prop('checked', $(this).prop("checked"));
});
Working example
To make sure the clear button always clear the selection, you should always set the property checked to false like this:
$(".clear").change(function () {
$(this).closest('.dropdown-menu').find('input:checkbox').prop('checked', false);
});
Updated example
It's only working for the first menu because an id is suppose to be unique. You are actually only applying an event listener to the first #clear element. In addition, you are toggling the checked property of all the checkboxes, not just the checkboxes in that dropdown menu.
Change the id attribute to a class, in this case, .clear, and when the change event is fired, select the closest .dropdown-menu element and toggle all the descendant checkbox elements:
Updated Example
$(".clear").on('change', function () {
$(this).closest('.dropdown-menu').find('input[type="checkbox"]').prop('checked', this.checked);
});
You could also just remove the checkbox and attach a click event to the .btn_clear element. You will also need to stop the event propagation in order to prevent the dropdown element from closing:
Updated Example
$(".btn_clear").on('click', function (e) {
e.stopPropagation();
$(this).closest('.dropdown-menu').find('input[type="checkbox"]').prop('checked', false);
});

Categories