Update: The script is now working properly I inserted a script into my website and it isn’t working. What should I fix to make a proper live demo of what’s going on. Let me explain the problem:
I know this code could be a lot shorter, but my knowledge of JavaScript is very limited and I am amazed I got this far.
It is almost working: when you select a size in the option dropdown, the relevant div becomes visible with display: flex and the div with id #item-cart-none hides. When you select another option in the same option dropdown, the div that became visible hides and the newly selected option shows another div. Meanwhile, the div with id #item-cart-none stays hidden.
When you select option “Geen” in the same dropdown the div with id #item-cart-none becomes visible again.
Now here is my problem: when you select a size in both dropdowns and select option “Geen” in one of the dropdowns, the div with id #item-cart-none becomes visible again. This should not happen. This div should only becomes visible again when nothing is selected in both of the dropdowns or option “Geen” is selected in both of them.
How can I fix my code so that this if-else statement, which currently exists in both of the functions (for both dropdowns) gets executed properly in regards to both dropdowns? I have fiddled with the code already myself, but it is mainly guesswork for me about what should work or not.
Edit: when you select a size in one of the dropdowns AND in the other dropdown, both relevant divs become visible. That is the whole point of this code. That when you add multiple prints to the “shopping cart” they become visible in the cart, and when none are selected you see the message “Geen” (which is Dutch and means “None” in English).
function print1SelectCheck(nameSelect)
{
if(nameSelect){
print1Option1Value = document.getElementById("13x19namiddag").value;
print1Option2Value = document.getElementById("20x30namiddag").value;
print1Option3Value = document.getElementById("30x45namiddag").value;
if(print1Option1Value == nameSelect.value){
document.getElementById("13x19namiddagdiv").style.display = "flex";
}
else{
document.getElementById("13x19namiddagdiv").style.display = "none";
}
if(print1Option2Value == nameSelect.value){
document.getElementById("20x30namiddagdiv").style.display = "flex";
}
else{
document.getElementById("20x30namiddagdiv").style.display = "none";
}
if(print1Option3Value == nameSelect.value){
document.getElementById("30x45namiddagdiv").style.display = "flex";
}
else{
document.getElementById("30x45namiddagdiv").style.display = "none";
}
if(print1Option1Value == nameSelect.value || print1Option2Value == nameSelect.value || print1Option3Value == nameSelect.value){
document.getElementById("Item-cart-none").style.display = "none";
}
else{
document.getElementById("Item-cart-none").style.display = "flex";
}
}
else{
document.getElementById("13x19namiddagdiv").style.display = "none";
document.getElementById("20x30namiddagdiv").style.display = "none";
document.getElementById("30x45namiddagdiv").style.display = "none";
document.getElementById("Item-cart-none").style.display = "flex";
}
}
function print2SelectCheck(nameSelect)
{
if(nameSelect){
print2Option1Value = document.getElementById("13x19onderweg").value;
print2Option2Value = document.getElementById("20x30onderweg").value;
print2Option3Value = document.getElementById("30x45onderweg").value;
if(print1Option1Value == nameSelect.value){
document.getElementById("13x19onderwegdiv").style.display = "flex";
}
else{
document.getElementById("13x19onderwegdiv").style.display = "none";
}
if(print2Option2Value == nameSelect.value){
document.getElementById("20x30onderwegdiv").style.display = "flex";
}
else{
document.getElementById("20x30onderwegdiv").style.display = "none";
}
if(print2Option3Value == nameSelect.value){
document.getElementById("30x45onderwegdiv").style.display = "flex";
}
else{
document.getElementById("30x45onderwegdiv").style.display = "none";
}
if(print2Option1Value == nameSelect.value || print2Option2Value == nameSelect.value || print2Option3Value == nameSelect.value){
document.getElementById("Item-cart-none").style.display = "none";
}
else{
document.getElementById("Item-cart-none").style.display = "flex";
}
}
else{
document.getElementById("13x19onderwegdiv").style.display = "none";
document.getElementById("20x30onderwegdiv").style.display = "none";
document.getElementById("30x45onderwegdiv").style.display = "none";
document.getElementById("Item-cart-none").style.display = "flex";
}
}
.item-cart {
display: none;
min-width: 100%;
margin-bottom: 0.5rem;
padding-top: 1rem;
padding-right: 1rem;
padding-bottom: 1rem;
padding-left: 1rem;
align-items: center;
grid-column-gap: 1rem;
grid-row-gap: 1rem;
border-top-left-radius: 12px;
border-top-right-radius: 12px;
border-bottom-left-radius: 12px;
border-bottom-right-radius: 12px;
background-color: hsla(0, 0.00%, 3.92%, 1.00);
color: #e6e6e6;
}
.item-cart-image {
height: 50px;
}
.small-text {
position: relative;
z-index: 10;
font-weight: 400;
font-size: 1rem;
}
.item-cart-none {
display: flex;
margin-bottom: 0.5rem;
padding-top: 1rem;
padding-right: 1rem;
padding-bottom: 1rem;
padding-left: 1rem;
align-items: center;
border-top-left-radius: 12px;
border-top-right-radius: 12px;
border-bottom-left-radius: 12px;
border-bottom-right-radius: 12px;
background-color: hsla(0, 0.00%, 3.92%, 1.00);
color: #e6e6e6;
}
<select id="Namiddag" name="Namiddag" data-name="Namiddag" class="select-field w-select" onchange="print1SelectCheck(this);">
<option value="Formaat">Selecteer formaat en bekijk prijzen</option>
<option id="13x19namiddag" value="13x19 cm / €12,50">13x19 cm / €12,50</option>
<option id="20x30namiddag" value="20x30 cm / €22,50">20x30 cm / €22,50</option>
<option id="30x45namiddag" value="30x45 cm / €32,50">30x45 cm / €32,50</option>
<option value="geen">geen</option>
</select>
<select id="Onderweg" name="Onderweg" data-name="Onderweg" class="select-field w-select" onchange="print2SelectCheck(this);">
<option value="Formaat">Selecteer formaat en bekijk prijzen</option>
<option id="13x19onderweg" value="13x19 cm / €12,50">13x19 cm / €12,50</option>
<option id="20x30onderweg" value="20x30 cm / €22,50">20x30 cm / €22,50</option>
<option id="30x45onderweg" value="30x45 cm / €32,50">30x45 cm / €32,50</option>
<option value="geen">geen</option>
</select>
<span id="Item-cart-none" class="item-cart-none">Geen</span>
<div id="13x19namiddagdiv" class="item-cart"><img class="item-cart-image" src="https://uploads-ssl.webflow.com/6296730d233387c2703a6964/62af4159b8d59a5cd322e5a8_film-photography-afternoon-silvan-soeters.jpg" alt="Afternoon"><span class="small-text">Afternoon - 13x19 cm / €12,50</span></div>
<div id="20x30namiddagdiv" class="item-cart"><img class="item-cart-image" src="https://uploads-ssl.webflow.com/6296730d233387c2703a6964/62af4159b8d59a5cd322e5a8_film-photography-afternoon-silvan-soeters.jpg" alt="Afternoon"><span class="small-text">Afternoon - 20x30 cm / €22,50</span></div>
<div id="30x45namiddagdiv" class="item-cart"><img class="item-cart-image" src="https://uploads-ssl.webflow.com/6296730d233387c2703a6964/62af4159b8d59a5cd322e5a8_film-photography-afternoon-silvan-soeters.jpg" alt="Afternoon"><span class="small-text">Afternoon - 30x45 cm / €32,50</span></div>
<div id="13x19onderwegdiv" class="item-cart"><img class="item-cart-image" src="https://uploads-ssl.webflow.com/6296730d233387c2703a6964/62af4158450de16f13120983_film-photography-commute-silvan-soeters.jpg" alt="Commute"><span class="small-text">Commute - 13x19 cm / €12,50</span></div>
<div id="20x30onderwegdiv" class="item-cart"><img class="item-cart-image" src="https://uploads-ssl.webflow.com/6296730d233387c2703a6964/62af4158450de16f13120983_film-photography-commute-silvan-soeters.jpg" alt="Commute"><span class="small-text">Commute - 20x30 cm / €22,50</span></div>
<div id="30x45onderwegdiv" class="item-cart"><img class="item-cart-image" src="https://uploads-ssl.webflow.com/6296730d233387c2703a6964/62af4158450de16f13120983_film-photography-commute-silvan-soeters.jpg" alt="Commute"><span class="small-text">Commute - 30x45 cm / €32,50</span></div>
#item-cart-nonebe visible when you haveFormaatin one<select>andgeenin the other?