Selecting a style sheet through a dropdown menu - html

So I have my site set up to have two different themes, and that works wonderfully, but when I'm making a lot of changes, I can miss some stuff and things start to not look the same.
This is how I'm doing that:
<select onchange="window.location=this.value">
<option>Default</option>
<option value="http://MyWebsite.com/Cyan/">Cyan</option>
</select>
What I WANT to do, is just have two separate style.css files and have the dropdown menu request the one you choose.
This would make my job so much easier, but I'm not 100% sure how to do it. Would I simply replace the website link with the stylesheet? Is there even a way to do this?

One possible solution using jquery:
$("select").on("change", function(){
$("<link/>", {
rel: "stylesheet",
type: "text/css",
href: $(this).val()
}).appendTo("head");
});

There's a framework that deals with this kind of thing. Coupled with jQuery it could be pretty good. It's utils.js, available there.
Your HTML should look like this:
<link id="stylesheet" rel="stylesheet" href="path/to/theme1.css" />
...
<select id="dropdown">
<option value="path/to/theme1.css">Theme 1</option>
<option value="path/to/theme2.css">Theme 2</option>
</select>
And then your script:
$("#dropdown").on("change", function(){
var path = $(this).val(); // Get path from selected option
utils.changeStylesheet("stylesheet", path);
});
Should work fine, though I haven't checked the documentation so the utils statement may need revising...

Related

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">[...]

Re: Linking to multiple places on a single page using a dropdown menu?

Ok, so I've been researching for some time now, without any luck, a way to to access different areas of a single page from a list of links in a dropdown sub-menu that currently contains five (5) working links that go to different pages.
What I'd like to do is keep the separate links for each piece of content, but combine the information from these pages to a single page and then link those specific areas from the existing dropdown menu / sub-menu links.
Can this be done? I understand about anchor tags and have tried to see how I can use them here but it doesn't appear to be an option since I'm not trying to link from within a page, rather from sub-menus in a dropdown menu?
Any thoughts or ideas would be greatly appreciated, if this is in fact possible! Thanks...
BTW: not sure if it's going to make a difference, but this site is in WordPress.
Use jquery and following code
HTML:
<select id="_selectPlatform" name="_selectPlatform" onChange=selectPlatformChange();>
<option value="default">Select</option>
<option value="Android">Android</option>
<option value="Blackberry">BlackBerry</option>
<option value="IOS">IOS</option>
<option value="Others">Others</option>
</select>
and script code
function selectPlatformChange() {
var _selectPlatform = $('#_selectPlatform').val();
if (_selectPlatform == 'Android') {
window.location.href = 'androidpage.html';
}
}
it will work

Dropdown Menu Options are Image (not text)

I want to ask about html dropdown menu. As we know, that option tag in html only support text. However, I need to create a drowp down menu that show list of image. I've searched for it. And I found several ways, including using css or jquery. However, that's not what I want. Here is, what I want to create:
<option value = blablabla><img src = "img/blbalba.txt"></option>
The code above is not working. Is there any sugestion to display dropdown menu like that?
Thank you for your help.
As You can't do that in plain HTML, but you can do it with JQuery.
Right now You can do like this
<select>
<option value="img1" style="background-image:url(images/img1.png);">image 1</option>
<option value="img2" style="background-image:url(images/img2.png);">image 2</option>
<option value="img3" style="background-image:url(images/img3.png);">image 3</option>
<option value="img4" style="background-image:url(images/img4.png);">image 4 </option>
</select>
Browsers will only render text inside a select tag, so, if you wan to use a selector that shows images instead of text, you cannot use a select you'll need to build your own selectable object.
That could be quite difficult to do. You can use different premade solutions that people on your situation developed before or try to reinvent the wheel.
Check this :)
A free light weight jQuery plugin that allows you to create a custom drop down with images and description.
What is so cool about this plugin!
Adds images and description to otherwise boring drop downs.
Allows JSON to populate the drop down options.
Converts your HTML select element to ddSlick.
Uses Minimum css and no external stylesheets to download.
Supports callback functions on selection.
Works as good even without images or description!
http://designwithpc.com/Plugins/ddSlick#demo

Chrome bug on select element dropdown when many options are hidden

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!

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>