Chrome bug on select element dropdown when many options are hidden - html

I'm looking for a workaround for a rendering bug in Chrome. It shows up when a select element has about 90%+ hidden option elements. In Chrome, the dropdown height becomes too short to use. This does not appear to happen on other browsers. View example on jsFiddle.
HTML Example
Note: Some options were removed to keep the code brief.
The bug does not show up unless all options are present.
100 Options, 90% Hidden:<br>
<select>
<option value="">Select an Option</option>
<option value="0" style="display: none">Option 0</option>
<option value="1" style="display: none">Option 1</option>
<option value="2" style="display: none">Option 2</option>
<option value="3" style="display: none">Option 3</option>
<!-- Options removed for brevity. -->
<option value="86" style="display: none">Option 86</option>
<option value="87" style="display: none">Option 87</option>
<option value="88" style="display: none">Option 88</option>
<option value="89" style="display: none">Option 89</option>
<option value="90">Option 90</option>
<option value="91">Option 91</option>
<option value="92">Option 92</option>
<option value="93">Option 93</option>
<option value="94">Option 94</option>
<option value="95">Option 95</option>
<option value="96">Option 96</option>
<option value="97">Option 97</option>
<option value="98">Option 98</option>
<option value="99">Option 99</option>
</select>
Browsers Tested:
Chrome 27 & 28 (Fail)
Firefox 21 (Pass)
IE 9 (Pass)
Opera 12 (Pass)
Safari 5.1 (Pass)
View Example on jsFiddle
Alternate Example Link
Update: I did some reading on the subject, and apparently options are not supposed to be hidden within a select. You can disable options, but they will not disappear. If you don't want an option to be in the select at all, you're supposed to remove the node entirely. The ability to hide options doesn't appear to work completely cross-browser, and in most you can continue to select the "hidden" options by using the arrow keys. I need to toggle options on and off, which makes this inconvenient to my particular situation, but this appears to be the only method that will work thus far.

Adding this might be considered a workaround:
$(document).ready(function () {
$('#ph2').mouseenter(function () {
var html = '';
$(this).find('option').each(function () {
if ($(this).css('display') !== 'none') {
html = html + '<option>' + $(this).text() + '</option>';
}
});
$(this).html(html);
})
});
Here's the jsFiddle; I'm using jquery just for simplicity. In this case, I'm just redoing the HTML on mouseenter. It's not ideal but it could get you going further. Also, note that you have ph2 set up as a div in your HTML; I think you should set it as a select element from the start and on the fiddle you can see the change I made to the html. But overall, until the bug is fixed, I think something like this is going to be as close as you'll get to having a working option.

The problem is already active on all major browser (Edge, Chrome, Opera,....) but Firefox.
The problem arises as soon as the number of hidden items is greater than 1000.
Be careful, because with just 100 items, all browser seem to work
The problem also disappears if the active items (not hidden, not disabled) are at the top of the list.
More exactly, when you click on the "select" input field, the browser open a list with a number of rows that is equal to the number of active items included in the first 1000 item.
For example, if you build a list of items with first x items active, then y items "inactive" (where y if greater than 1000) and then again z items active, you will see a list wide x rows with x+z items.
By the way, a workaround to the problem could be the sorting of the items list
This behaviour has been verified on Chrome, Opera, Edge

As a workaround for this bug I can propose the following solution:
To hide an option 'convert' it to some other tag and hide it with .hide().
To show an option 'convert' it back to option and show it with .show().
For 'conversion' we need something like this: https://stackoverflow.com/a/9468280.
Example:
// some `replaceTagName` implementation here
// hiding options
$('.option-selector').replaceTagName('span').hide();
// showing options
$('.option-selector').replaceTagName('option').show();
A bit heavy but working :)

I had this problem and this is what I did:
var originalSelect = $("#the_select").clone();
function hideSomeOptions(){
$('#the_select .hide_me').remove();
}
function restoreAllOptions(){
$("#the_select").replaceWith(originalSelect[0]);
}
The target select input has the ID "the_select" and the options or optgroups that need to be toggled have the "hide_me" class.

I found that the order of the hidden/visible options make a difference. It is like chrome stops counting for the height for the drop down at the first hidden option. One way around is to move the shown options to the top of the select. If you are using jquery something like this.
var select = "select#MySelect";
$(select).children("option").hide(); //hide all options
$(select).children("Selector for items to show").each(function(idx, elm) {
$(elm).parent().prepend(elm); //move item to the top of parent
});
$(select).children("Selector for items to show").show(); //show selected options

I ran into the same problem (Chrome 40) and found that the following workaround works well for me.
var originalOptions = [];
$(document).ready(function(){
originalOptions = $("yourSelect").children("option");
$("someElement").someEvent(function(){
$("yourSelect").children("option").remove();
$(originalOptions).each(function(){
if(/*criteria here*/){$("yourSelect").append($(this));}
});
});
});

The best fix is to add at the end on the last
<option></option> in your Select Element. Add this code:
<optgroup></optgroup>
This will add a blank group element, and for now is the best Easy and fast FIX to this rare BUG.
Thanks!

Related

How to do pagination in select box based on scroll

HTML
<select class="form-control choice requiredField" id-available name="SelectData" ng-model="box.box_id" title="Select">
<option value="" disabled selected>{{'_SELECT_BEACON_' | i18n}}</option>
<option value="{{y.box_id}}" ng-repeat="y in dataList" class="nms">{{y.beacon_code}}</option>
</select>
ANGULARJS
boxService.listUnassignedDevice(function(res) {
$scope.dataList = res;
}, function(error) {
});
Here I have large data in "dataList"(10000>). So I need to list data in select box after mouse scroll down. First I have to show 100 data, when mouse scroll down, I need to show next 100 data from API.
you can use ngInfiniteScroll directive and add script tag then add module 'infinite-scroll'and then Use the directive by specifying an infinite-scroll attribute on an element.
<div infinite-scroll="myPagingFunction()" infinite-scroll-distance="3"></div>
ngInfiniteScroll will call myPagingFunction() any time the bottom of
the element approaches the bottom of the browser window. See the API
documentation for more information about what attributes are available
for use, and take a look at the demos to see ngInfiniteScroll in
action.

CSS for <select> menu works ... sometimes

This is bizarre. I have a select menu that I've styled to display a hierarchical list.
I have multiple Firefox profiles setup to simulate multiple sessions. One is my "main" profile with numerous extensions and customizations.
However, this is what my styled menu looks like in my "main" profile while using safe mode:
(EDIT: Firefox's safe mode that disables the extensions and resets the configuration)
This is what it looks like in a clean, de-cached Firefox profile:
For comparison's sake, Chrome and IE make it half-way there:
I've already got -moz-appearance: none and -webkit-appearance:none; for both the <select> and <option> elements. The left offset on the options is achieved using padding-left.
The most perplexing thing is how this works in one Firefox profile (in safe mode) but not a completely clean one.
How do I make this consistently like the first example across all browsers?
Edit: fiddle
It seems to me you have 3 options:
Pure Html:
Add in blank non-breaking spaces before your options in the lower levels and do it that way : not exactly kind to your time, and a bit messy-looking, but would work.
Pure CSS:
Add on an additional class depending on the level ( doesn't seem to work - already tried it )
Jquery/Javascript: Probably the quickest way! If you are willing to use jquery [or you could do it in plain js, but I've done the fiddle using jquery..]:
Add in a hierarchy indicator using additional classes, depending on the level of the hierarchy See fiddle or snippet
$(document).ready(
function() {
$('.l2').each(
function() {
$(this).text('--' + $(this).text());
}
);
$('.l3').each(
function() {
$(this).text('----' + $(this).text());
}
);
}
);
#hierarchy,
#hierarchy>option {
text-align: justify;
text-shadow: 1px 1px grey; /*optional!*/
word-spacing: 3px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="hierarchy" name="hierarchy">
<option class="l1" selected>Level 1</option>
<option class="l2">First L2 Indent</option>
<option class="l3">First L3 Indent</option>
<option class="l2">Second L2 Indent</option>
<option class="l3">Second L3 Indent</option>
<option class="l3">Third L3 Indent</option>
</select>

HTML Dropdown Value changing on Page Refresh

Ok, so this is a wierd one and have not seen it before.
I have a simple html dropdown w/ no js attached (yet).
This is using html4(xhtml1?) with transitional doctype.
btw.. i tried with escaping slashes and without, same results. (/)
<select id="myDropdown" name="myDropdown">
<option value="option1" selected="selected">Contact / Email Customer Care</option>
<option value="option2">Group / Private Tour</option>
<option value="option3">Option3</option>
<option value="option4">Option4</option>
<option value="option5">Option5</option>
</select>
When I do a hard reload (ctrl+f5, or ctrl+shift+R) It loads the correct option in the box (option 1).
However, if I hit just plain F5 for a soft refresh, it loads option2 as the selected option ONLY if option 1 is selected. If option 3, 4 or 5 are selected, it doesn't change. But if option 1 is selected, it changes to option 2 every time on a page refresh.
Any ideas why this is happening? It's extremely frustrating and I don't see anything about it online.
Thanks
It appears to be a strange Firefox behavior, I can see it in versions of Firefox up to and including 27 (current). I'd suggest logging a bug/sending feedback.
to solve the issue though is fairly simple... if you want the drop-down to always reset on a page load just a simple bit of script after the combo will force that (and doesn't have a negative impact on other browsers
<script>
// console.log(document.getElementById("myDropdown").value)
document.getElementById("myDropdown").selectedIndex = 0
</script>
if the selected value is likely to change, then you'll want the selectedIndex to match whatever value you're adjusting the selected to indicate.
Oh, and just a note on the selected="selected" ... that attribute would usually be just selected but
<option value="option1" selected>Contact / Email Customer Care</option>
doesn't make a difference here
Same idea as in the answer by Offbeatmammal but a bit more automated, assuming that you can use the same attribute on all the selectors in the page (else restrict using a class or id):
<script>
function fix_selectors_positions()
{
selectors = document.querySelectorAll("select");
selectors.forEach(item =>
item.selectedIndex = item.getAttribute("default_pos")
);
}
window.onload = fix_selectors_positions;
</script>
And then use the attribute default_pos on the selectors:
<select default_pos="0">[...]

Multiple select in Safari iOS 7

When I use the multiple option in a select dropdown - safari runs into weird issues. When I select an option and say Done, the dropdown goes back to showing '0 items'. But if I select multiple options (more than one), everything except the first one gets selected. After this, if I unselect all options, the last one remains selected.
Check this for a demo using safari on iOS 7.0.3.
<select multiple="multiple">
<option value="1">option 1</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
</select>
I've looked at http://www.thecssninja.com/html/optgroup-ios6, but that talks about issues with using optgroups - which(when used with multiple) currently seems to crash safari altogether.
// hack for iPhone 7.0.3 multiselects bug
if(navigator.userAgent.match(/iPhone/i)) {
$('select[multiple]').each(function(){
var select = $(this).on({
"focusout": function(){
var values = select.val() || [];
setTimeout(function(){
select.val(values.length ? values : ['']).change();
}, 1000);
}
});
var firstOption = '<option value="" disabled="disabled"';
firstOption += (select.val() || []).length > 0 ? '' : ' selected="selected"';
firstOption += '>« Select ' + (select.attr('title') || 'Options') + ' »';
firstOption += '</option>';
select.prepend(firstOption);
});
}
Simple add:
<option disabled></option>
as the first element of multiple select.
This has partly been fixed in 7.1 that was released the other day, however, there are still many issues. Item count is now correct, but...
you can select optgroup titles (you should not be able to do this, and if so, it should at least select/unselect the entire group.
if you disable an option <option disabled="disabled">Computer 1</option> you can still select it which of course it totally wrong as well.
Get it together Apple.
'Multiple select' bugs in Safari in iOS 7.0.3 on the iPhone have been reported by others, as well, on Apple's discussion forums; e.g.:
https://discussions.apple.com/message/23745665#23745665
https://discussions.apple.com/message/23607781#23607781
Since it's Apple that will need to fix this, the consensus approach for what you can do to help facilitate resolution of this issue, per posts on those two discussion threads, is to:
Look for existing bugs and, if necessary, file a new bug, via Apple's Bug Reporter.
Join any existing discussion topic(s) about this in Apple's Developer Forums, and open a new topic if this has not yet been discussed. (The discussions above both took place in Apple's public forums, in the "Using iPhone" area, and as such might not necessarily be seen or responded to by Apple's Developer Support staff.)
I seem to have come up with a fix with mysteriously works with jQuery. I imagine you could vanilla-ify the code if you don't want the jQuery dependency:
/**
* iOS mutliple select fix.
*/
if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
$('select[multiple]').each(function() {
$(this).prepend('<option disabled></option>');
$(this).append('<optgroup disabled></optgroup>');
});
}

Using page anchors in <Select> dropdown list in html

I have a static HTML page that contains a lot of tables. I would like to use the html tag <select> to create a simple dropdown menu which jumps to a specific table further down the page.
What command sets the user selection to jump down the page and what anchor code is required to give that selection a target.
I believe you just need to use JavaScript to make it so when you click/change value on the select box it takes you to the link specified in the value of that select option.
<select name="dropdpown" size="1" id="select-anchor">
<option value="#link">foo</option>
<option value="#link">bar</option>
</select>
And using jQuery library for the JavaScript functionality simplifies things (and will other JS related features of your web project)
$(document).ready(function () {
$('#select-anchor').change( function () {
var targetPosition = $($(this).val()).offset().top;
$('html,body').animate({ scrollTop: targetPosition}, 'slow');
});
});
Here's a jsFiddle of it in action.
You can't do that without JavaScript. Nor you should.
I, as a user, expect to select a value from a dropdown list. Select one item between others. Not act as navigation.
One can argue that it's OK if done for a mobile browser, but that's still bad UX.
This is how it can go to a link on select:
<select onchange="window.location.href=this.value" >
<option value="search.php?perpage=10">10</option>
<option value="search.php?perpage=20">20</option>
<option value="search.php?perpage=50">50</option>
<option value="search.php?perpage=100">100</option>
</select>