Jess Kumanovski
Jess Kumanovski

Reputation: 1

Keep button in active state until clicked off

This is the page I'm working on: https://www.maisondefemmes.com/product/choose-your-own-adventure-earrings/

I want the buttons to stay in an active state until unclicked so that the user can clearly see what they have selected.

I have read and followed these instructions, to no avail: Keep button in active state until clicked upon again

NB: The 'button' used to be a toggle switch that I have turned off. Not sure if this is causing an issue.

I've added this JQuery to my theme:

$('.bundled_product_optional_checkbox').click(function(){
        if($(this).hasClass('active')){
            $(this).removeClass('active')
        } else {
            $(this).addClass('active')
        }
    });

As well as this CSS:

.bundled_product_optional_checkbox.active {
	background-color: #cfb87c !important;
	border: 1px solid #cfb87c !important;
}

<label class="bundled_product_optional_checkbox">
  <input class="bundled_product_checkbox" type="checkbox" name="component_1592104877_bundle_selected_optional_4" value>
  " Add for "
  <span class="price">
    <span class="woocommerce-Price-amount amount">
      <span class="woocommerce-Price-currencySymbol">$</span>
    </span>
  </span>
    ::after
</label>

Appreciate any answers. Please be kind.

Upvotes: 0

Views: 1359

Answers (2)

Prabodh
Prabodh

Reputation: 175

Just create your own class called active

<button class="Toggle Button">Toggle Button</button>

then create your own class active and style it in css

.active{
            border: none;
            background: teal;
            color: white;
        }

Now in jQuery toggle the active class on click

$('.toggle-button').click( function() {
    $('.toggle-button').toggleClass('active');
});

Now you should have a working toggle switch

Here is the link to my code pen: https://codepen.io/prabodhpanda/pen/pogNqpQ

Upvotes: 1

Maulik
Maulik

Reputation: 97

answer updated..

if you are not using bootstrap then you can ignore those classes and CDN links : )

$('.bundled_product_optional_checkbox').click(function(){
        if($(this).hasClass('active')){
            $(this).removeClass('active')
        } else {
            $(this).addClass('active')
        }
    });
.bundled_product_optional_checkbox.active {
	background-color: #cfb87c !important;
	border: 1px solid #cfb87c !important;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>



<button class="btn btn-lg btn-default border bundled_product_optional_checkbox">Click me</button>

Upvotes: 0

Related Questions