In its simplest form (without a database see code below). If you have dozens of hats with dozens of colors then really the only way is to store the information is in a database, for easy management.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hat Choice</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<script>
$(document).ready(function(){
$('.hat_colors').hide();
$('.hat_option').change(function(){
let hat_option = $('.hat_option').val();
if(hat_option === "Small") {
$('.hat_colors').show().html(
'<option value="" selected>Select Color</option>' +
'<option value="Pink">Pink</option>' +
'<option value="Red">Red</option>' +
'<option value="Green">Green</option>'
)
}
if(hat_option === "Medium") {
$('.hat_colors').show().html(
'<option value="" selected>Select Color</option>' +
'<option value="Orange">Orange</option>' +
'<option value="Mauve">Mauve</option>' +
'<option value="Black">Black</option>'
)
}
if(hat_option === "Large") {
$('.hat_colors').show().html(
'<option value="" selected>Select Color</option>' +
'<option value="Violet">Violet</option>' +
'<option value="Grey">Grey</option>' +
'<option value="Brown">Brown</option>'
)
}
});
});
</script>
</head>
<body>
<select name="hat_option" class="hat_option">
<option value="" selected>Select Option</option>
<option value="Small">Small</option>
<option value="Medium">Medium</option>
<option value="Large">Large</option>
</select>
<select name="hat_colors" class="hat_colors"></select>
</body>
</html>
You could also use the jquery load function to keep the code a bit more managable if you are trying to do this using a non-database workflow. You would create seperate html files with the color options related to each hat and load the file based on which hat option was chosen
<script>
$(document).ready(function(){
$('.hat_colors').hide();
$('.hat_option').change(function(){
let hat_option = $('.hat_option').val();
if(hat_option === "Small") {
$('.hat_colors').show().load('small_color_options.html')
}
if(hat_option === "Medium") {
$('.hat_colors').show().load('medium_color_options.html')
}
if(hat_option === "Large") {
$('.hat_colors').show().load('large_color_options.html')
}
});
});
</script>
So for example in 'small_color_options.html' you would have the below code:
<option value="" selected>Select Option</option>
<option value="Pink">Pink</option>
<option value="Red">Red</option>
<option value="Green">Green</option>