I’m trying to show/hide the cart button depending on 2 separate select field values.
My logic is this:
Display the cart button if selectId #pa_custom-engraving = 'no'
OR if selectId #pa-color != 'custom-print'
. Otherwise I want to hide the cart.
This is what I have so far which works unless you continue to toggle the select fields they cancel each other out. How can I combine this into proper ‘OR’ conditional statement?
JS
document.getElementById('pa_custom-engraving').addEventListener('change', function () {
var style = this.value == 'no' ? 'block' : 'none';
document.getElementsByClassName('woocommerce-variation-add-to-cart')[0].style.display = style;
});
document.getElementById('pa_color').addEventListener('change', function () {
var pstyle = this.value !== 'custom-print' ? 'block' : 'none';
document.getElementsByClassName('woocommerce-variation-add-to-cart')[0].style.display = pstyle;
});
HTML
<select id="pa_custom-engraving">
<option value="">Choose an option</option>
<option value="no">No</option>
<option value="yes">Yes</option>
</select>
<select id="pa_color">
<option value="">Choose an option</option>
<option value="black">Black</option>
<option value="white">White</option>
<option value="custom-print">Custom Print</option>
</select>
2
Answers
Add the same listener to both selects. On change, look at both select values, and apply the conditions separated by
||
:You can take the all selected value in an array and check if the array includes/not include the value you want to match: