At the moment the customer can enter a Birthdate and Initials but we need to only allow them to select one based on what Monogramming they select in the dropdown.
Each dropdown if different for each item and Im not sure the best JS to achieve this is.
As you’re using OpenCart on the backend, the most secure solution is for server-side PHP to filter out the initials or birthdate based on what has been ordered. You can do that as an include, which can also then be used by JavaScript as an ajax request, to find out what is allowed too.
That way you have the one central “business logic” area that determines what is allowed.
The basic rule is to use the PHP server-side code to determine what is allowed, and only once that is working to then have JavaScript interact with the PHP to help improve the user experience.
Attempting to improve the user experience first tends to result in catastrophic failure. Some examples are where the JavaScript ordering allows people to give themselves negative prices for items.
Get the server-side PHP working appropriately first, and then use Ajax to connect the JavaScript side up to the PHP code. That’s the recommendation here.
I can hide them on load easy enough but getting the IF ELSE statement to work is tricky.
// when the page loads
$( ".option-date" ).hide();
$( ".option-text" ).hide();
So if Initials is selected from Monogramming show the DIV option-text
$( ".option-text" ).show();
or if DOB is selected from Monogramming show the DIV option-date
$( ".option-date" ).show();
I thought about approaching this with PHP but I think JQuery might be easier.
I’m wondering should I get the text first as the option id/class is different for each product page
Then if it equals DOB or Initials then show “option-text” or “option-date”
Tried this but didn’t seem to work
<script>
$(function() { // Makes sure the code contained doesn't run until
// all the DOM elements have loaded
$('#input-option{{ option.product_option_id }}').change(function(){
$('.option-text').hide();
$('.option-date').hide();
$('#input-option{{ option.product_option_id }}' + $(this).val()).show();
});
});
</script>
JavaScript (including jQuery) are not the primary solution for your problem. I also see that you haven’t yet attempted to approach the PHP forum about this either.