I would like to hide the custom size input table when the size selected is anything but Custom.
Test product https://scale-11.com/product/test-2/
Sizes boxes:
<li aria-checked="true" tabindex="0" data-wvstooltip="Custom" class="variable-item button-variable-item button-variable-item-custom selected" title="Custom" data-title="Custom" data-value="custom" role="radio" data-wvstooltip-out-of-stock=""><div class="variable-item-contents"><span class="variable-item-span variable-item-span-button">Custom</span></div></li>
<li aria-checked="false" tabindex="0" data-wvstooltip="XS" class="variable-item button-variable-item button-variable-item-xs" title="XS" data-title="XS" data-value="xs" role="radio" data-wvstooltip-out-of-stock=""><div class="variable-item-contents"><span class="variable-item-span variable-item-span-button">XS</span></div></li>
<li aria-checked="false" tabindex="0" data-wvstooltip="S" class="variable-item button-variable-item button-variable-item-s" title="S" data-title="S" data-value="s" role="radio" data-wvstooltip-out-of-stock=""><div class="variable-item-contents"><span class="variable-item-span variable-item-span-button">S</span></div></li>
<li aria-checked="false" tabindex="0" data-wvstooltip="M" class="variable-item button-variable-item button-variable-item-m" title="M" data-title="M" data-value="m" role="radio" data-wvstooltip-out-of-stock=""><div class="variable-item-contents"><span class="variable-item-span variable-item-span-button">M</span></div></li>
<li aria-checked="false" tabindex="0" data-wvstooltip="L" class="variable-item button-variable-item button-variable-item-l" title="L" data-title="L" data-value="l" role="radio" data-wvstooltip-out-of-stock=""><div class="variable-item-contents"><span class="variable-item-span variable-item-span-button">L</span></div></li>
Custom size input box
<div class="wapf-field-group" data-group="p_2333">
<div class="wapf-field-row">
<div class="wapf-field-container wapf-field-textarea" style="width:100%;" for="643aefd5dc8e8">
<div class="wapf-field-label wapf--above"><label><span>Custom size</span> <span class="wapf-pricing-hint">(+€35.00)</span></label></div><div class="wapf-field-description">Please input your custom size.<br> Please note you will not be able to return it. </div>
<div class="wapf-field-input">
<textarea name="wapf[field_643aefd5dc8e8]" class="wapf-input" data-is-required="" data-field-id="643aefd5dc8e8" data-wapf-price="35" data-wapf-pricetype="fixed"></textarea>
</div>
</div>
</div>
</div>
<script>
const name = document.querySelector(".wapf-field-group");
const btn = document.querySelector(".button-variable-item-s, .button-variable-item-S,.button-variable-item-m, .button-variable-item-l");
btn.addEventListener("click", dontDisplay);
function dontDisplay() {
name.style.display = "none";
}
const form = document.querySelector(".wapf-field-group");
const customButton = document.querySelector(".button-variable-item-custom");
btn.addEventListener("click", display);
function display() {
customButton.style.display = "block";
}
</script>
What happens now is that when I enter the product the table is visible and disappears when size S is selected and does not appear again when needed?
2
Answers
Why do you initiate two times your form ? It’s initiate in name and then form.
And the second time you change the display of the button and not the form.
One approach to the problem is below, with explanatory comments in the code:
JS Fiddle demo.
References:
Array.prototype.forEach()
.Array.prototype.filter()
.document.querySelector()
.document.querySelectorAll()
.Element.children
.Element.getAttribute()
.Element.setAttribute()
.Element.querySelector()
.Element.querySelectorAll()
.Event
.EventTarget.addEventListener()
.EventTarget.dispatchEvent()
.HTMLElement.dataset
API.HTMLElement.style
.Node.parentNode
.Number
.