omukiguy
omukiguy

Reputation: 1437

Change default radio buttons checked attribute on multi modal pop in

By default, Woocommerce gives me options for my product custom layout. On a click of a button like below

click of a button

I get a pop up modal with options default product for my pop up modal

However the default choice is always small size irrespective of the web user clicking large. I have attached demo code to showcase issue and my would be solution. I would like to use JS to remove the default checked on page load. Then on click of the button, the size will determine the main meal choice by using data-keys on html.

Issue: My large button click works but the small button click does not change the value checked. Your help or a different take would be appreciated.

window.onload = removePreSelection();
const clickBtns = Array.from(document.querySelectorAll('.defaultbtn'));
clickBtns.forEach(clickBtn => clickBtn.addEventListener('click', clickedBtnSelect));

//Remove the default selection of radio buttons on page.
function removePreSelection() {
  const inputs = document.querySelectorAll('input[name=attribute_pa_sizes]:checked');

  for (let i = 0; i < inputs.length; i++) {
    const element = inputs[i];
    element.removeAttribute("checked");  
  }
}

//Filter the values of the clicked button.
function clickedBtnSelect(e) {
  removePreSelection();
  const bentoSize = e.target.dataset.bento;
  const bentoModal = e.target.dataset.target.replace(/([#]+)/gi, '');
  const modalInputValue = document.querySelector('input[value=' + bentoSize + ']');
  console.log(modalInputValue);

  //set the attribute to checked.
  modalInputValue.setAttribute("checked", "checked");;

}
<div class="show-pricing">
  <p class="variable-pricing">
    <span>Large: 27500</span>
    <button class="btn defaultbtn" data-target="#myModal1900" data-toggle="modal" data-bento="large" type="button">Large</button>
  </p>
  <p class="variable-pricing">
    <span>Small: 23000</span>
    <button class="btn defaultbtn" data-target="#myModal1908" data-toggle="modal" data-bento="small" type="button">Small</button>
  </p>

  <!-- Modal -->
  <div aria-labelledby="myModalLabel" class="modal fade" id="myModal1900" role="dialog" tabindex="-1">
    <div class="variations">
      <div class="col-md-4">
        <h3 class="variations-options-head">Large Modal selected</h3>
        <div class="value">
          <fieldset>
            <p>
              <input id="pa_sizes" name="attribute_pa_sizes" type="radio" value="large">
              <span class="radio-span">Large</span>
            </p>
            <p>
              <input checked="checked" id="pa_sizes" name="attribute_pa_sizes" type="radio" value="small">
              <span class="radio-span">Small</span>
            </p>
          </fieldset>
        </div>
      </div>
    </div>
  </div>

  <!-- Modal 2 -->
  <div aria-labelledby="myModalLabel" class="modal fade" id="myModal1908" role="dialog" tabindex="-1">
    <div class="variations">
      <div class="col-md-4">
        <h3 class="variations-options-head">Small modal selected</h3>
        <div class="value">
          <fieldset>
            <p>
              <input id="pa_sizes" name="attribute_pa_sizes" type="radio" value="large">
              <span class="radio-span">Large</span>
            </p>
            <p>
              <input checked="checked" id="pa_sizes" name="attribute_pa_sizes" type="radio" value="small">
              <span class="radio-span">Small</span>
            </p>
          </fieldset>
        </div>
      </div>
    </div>
  </div>
</div>

Upvotes: 2

Views: 686

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254211

Try this working version made with jQuery instead, more compact and efficient:

(function($){
    // variables initialization
    var a = 'button[data-bento^="small"]',
        b = 'button[data-bento^="large"]',
        c = 'input[value^="small"]',
        d = 'input[value^="large"]';
        $(a).click(function(){
            var e = $(this).attr('data-target') + ' ' + c;
            console.log($(this).attr('data-target'));
            $(e).prop('checked', true);
        });
        $(b).click(function(){
            var f = $(this).attr('data-target') + ' ' + d;
            console.log($(this).attr('data-target'));
            $(f).prop('checked', true);
        });
})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="show-pricing">
  <p class="variable-pricing">
    <span>Large: 27500</span>
    <button class="btn defaultbtn" data-target="#myModal1900" data-toggle="modal" data-bento="large" type="button">Large</button>
  </p>
  <p class="variable-pricing">
    <span>Small: 23000</span>
    <button class="btn defaultbtn" data-target="#myModal1908" data-toggle="modal" data-bento="small" type="button">Small</button>
  </p>

  <!-- Modal -->
  <div aria-labelledby="myModalLabel" class="modal fade" id="myModal1900" role="dialog" tabindex="-1">
    <div class="variations">
      <div class="col-md-4">
        <h3 class="variations-options-head">Large Modal selected</h3>
        <div class="value">
          <fieldset>
            <p>
              <input id="pa_sizes" name="attribute_pa_sizes" type="radio" value="large">
              <span class="radio-span">Large</span>
            </p>
            <p>
              <input checked="checked" id="pa_sizes" name="attribute_pa_sizes" type="radio" value="small">
              <span class="radio-span">Small</span>
            </p>
          </fieldset>
        </div>
      </div>
    </div>
  </div>

  <!-- Modal 2 -->
  <div aria-labelledby="myModalLabel" class="modal fade" id="myModal1908" role="dialog" tabindex="-1">
    <div class="variations">
      <div class="col-md-4">
        <h3 class="variations-options-head">Small modal selected</h3>
        <div class="value">
          <fieldset>
            <p>
              <input id="pa_sizes" name="attribute_pa_sizes" type="radio" value="large">
              <span class="radio-span">Large</span>
            </p>
            <p>
              <input checked="checked" id="pa_sizes" name="attribute_pa_sizes" type="radio" value="small">
              <span class="radio-span">Small</span>
            </p>
          </fieldset>
        </div>
      </div>
    </div>
  </div>
</div>

You can click on "Run code snippet" button directly to see it in action…

Tested and works.


You can also embed it in a hooked function, this way:

add_filter( 'wp_footer','custom_product_title_script' );
function custom_product_title_script(){
    if( ! is_checkout() || is_admin() ) return; // only on single product pages
    ?>
        <script type="text/javascript">
        // HERE goes the jQuery script
        </script>
    <?php
}

Code goes in function.php file of your active child theme (or theme)…

Upvotes: 1

Related Questions