I want to change prices via select(onchange) for different stores. Each store as a product div and products inside with different the price structures included which I want to show or hide depending on which Store you choose.
Currently, the price only changes in the first product. I think it might be a loop/array problem but nothing I have tried, works.
I do have JQuery in the page, so solutions with JQuery are fine.
function priceStore(){
var option = document.getElementById("Store").value;
if((option=="Store1")||(option=="Store3")){
const elements = document.querySelectorAll(".product");
elements.forEach(function (element) {
document.getElementsByClassName('price1')[0].style.display = 'block';
document.getElementsByClassName('price2')[0].style.display = 'none';
document.getElementsByClassName('price3')[0].style.display = 'none';
});
}
if((option=="Store2")||(option=="Store4")|| (option=="Store5")){
const elements = document.querySelectorAll(".product");
elements.forEach(function (element) {
document.getElementsByClassName('price1')[0].style.display = 'none';
document.getElementsByClassName('price2')[0].style.display = 'block';
document.getElementsByClassName('price3')[0].style.display = 'none';
});
}
if((option=="Store6")){
const elements = document.querySelectorAll(".product");
elements.forEach(function (element) {
document.getElementsByClassName('price1')[0].style.display = 'none';
document.getElementsByClassName('price2')[0].style.display = 'none';
document.getElementsByClassName('price3')[0].style.display = 'block';
});
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<form method="post" enctype="multipart/form-data">
<select name="Store" id="Store" onchange="priceStore()">
<option disabled selected value="">choose store</option>
<option value="Store1">Store1</option>
<option value="Store2">Store2</option>
<option value="Store3">Store3</option>
<option value="Store4">Store3</option>
<option value="Store5">Store3</option>
<option value="Store6">Store3</option>
</select>
<div class="productrange">
<div class="product product1">
<h2>Product 1</h2>
<div class="price1">price1</div>
<div class="price2">price2</div>
<div class="price3">price3</div>
</div>
<div class="product product2">
<h2>Product 2</h2>
<div class="price1">price1</div>
<div class="price2">price2</div>
<div class="price3">price3</div>
</div>
<div class="product product3">
<h2>Product 3</h2>
<div class="price1">price1</div>
<div class="price2">price2</div>
<div class="price3">price3</div>
</div>
</div>
</form>