For your first question, you can add all categories separated by commas, for example in the select options. Then use this information to show what you need.
Html
<span>Items: </span><br />
<select id="item-filter">
<option value="blank"></option>
<option value="greens,reds,fruit">Apple</option>
<option value="greens,vegetable">Asparagus</option>
<option value="fruit">Banana</option>
<option value="reds">Car</option>
</select>
JavaScript
$(".headline_box").click(function(){$(this).css('background-color','initial')});
function selectCategory(category) {
if (category.attr('id') == 'category-filter') {
$('#item-filter').val('blank');
}
else if (category.attr('id') == 'item-filter') {
$('#category-filter').val('blank');
}
$(".category").hide();
var depts = category.val().split(',');
$.each(depts,function(index,value){
$(".category-" + value).show();
});
$(".headline_box").css('background-color','initial');
var selected_item = category.find("option:selected").text();
$("h4:contains('"+selected_item+"')").parents(".headline_box").css('background-color','#fae6cf');
}
I have unlocked the Code Pen here .
For your second question, could you please explain this better? I don't understand what you are trying to do.
UPDATE
To do what you suggested in the comments, try this:
Add this code AFTER you have added the pick pick function:
var preselectedCategory = "";
var preselectedItem = "Asparagus";
if(preselectedCategory && preselectedItem )
alert("System Error: You can only preselect Category OR Items");
else if(preselectedCategory){
var select_val = $("#category-filter option").filter(function () { return $(this).html() == preselectedCategory; }).val();
$("#category-filter").val(select_val).change();
}
else if(preselectedItem){
var select_val = $("#item-filter option").filter(function () { return $(this).html() == preselectedItem; }).val();
$("#item-filter").val(select_val).change();
}
Pay attention to some things:
- The default is selected by the
text
NOT optionvalue
- You cannot select the default category and item, this will raise a warning.
Here is the updated Code Pen
Literature:
source
to share