skip to Main Content

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.

2

Answers


  1. 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>
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search