I have a dropdown setup for searching. The idea is you click one option, Then the OPTGROUP appears. What I'd like to do is once the label is selected it jumps to that section of the OPTGROUP.
I have written some jQuery so far. See this jsFiddle - So once you select a main category. I would like the optgroup to go to its paired label. How can I achieve this?
HTML
<select id="category" name="category">
<option selected="selected" value="0">Choose a category...</option>
...
<option value="4">Photography</option>
</select>
<select multiple="multiple" id="subcategory" name="category_ids[]">
<optgroup label="Art Styles">
<option value="5">Abstract</option>
...
<option value="18">Graphics</option>
</optgroup>
<optgroup label="Art Subjects">
<option value="19">Animals</option>
...
<option value="45">Dreams & Nighmares</option>
</optgroup>
<optgroup label="Media">
<option value="46">Water Colour</option>
...
<option value="56">Mixed Media</option>
</optgroup>
<optgroup label="Photography">
<option value="57">Color Photography</option>
...
<option value="64">Celebrity Photography</option>
</optgroup>
Portrait Landscape Square
JavaScript
// Search Options Dropdown
// Hide Subcategory Dropdown
$('#subcategory').css('display', 'none');
// Hide Orientation Dropdown
$('#orientation').css('display','none');
$('#category').change(function()
{
$('#subcategory').css('display', 'block');
$('#orientation').css('display', 'block');
var CatValue = $("#category").val();
if (CatValue == '1')
{
}
});
The <optgroup> tag is used to group related options in a <select> element (drop-down list). If you have a long list of options, groups of related options are easier to handle for a user.
From Select2 docs: "Furthermore, <optgroup> elements cannot be made selectable. This is a limitation of the HTML specification and is not a limitation that Select2 can overcome."
Well, it feels a bit hacky, but it works in Chrome, and I am sure with a little browser sniffing and determining option item heights in each browser you could get it working in all of them:
// Search Options Dropdown
// Hide Subcategory Dropdown
$('#subcategory').css('display', 'none');
// Hide Orientation Dropdown
$('#orientation').css('display', 'none');
$('#category').change(function() {
var cat = $("#category :selected").text();
var $subcat = $('#subcategory');
$('#orientation').css('display', 'block');
var optcount = 0;
var optheight = 17;
$subcat.css('display', 'block').find('optgroup').each(function() {
optcount++;
if ($(this).attr('label') == cat) {
$subcat.val($('#subcategory optgroup[label="' + $(this).attr('label') + '"]').children('option:first').val())
.scrollTop($('#subcategory :selected')[0].index * optheight + ((optcount - 1) * optheight));
}
});
});
jsfiddle
I am sure there is a little cleanup you could do too - like move optgroup++ and remove the - 1 from scrollTop - ewww. It should get you pointed in the correct direction at least.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With