HTML drop down list - html

How do I create a html drop down list that refreshes back to the option disabled tag once an individual has selected an option value, has viewed their selection and has decided to go back to make another option value selection.
html:
<form>
<select name="URL" onchange="window.location.href=this.form.URL.options[this.form.URL.selectedIndex].value">
<option disabled="disabled" selected="selected">Choose a County for Recycling Information.......</option>
<option value="LINK">Ingham County (Google Map / PDF)</option>
<option value="LINK">Clinton County (PDF)</option>
<option value="LINK">Eaton County (PDF)</option>
</select>
</form>
The -select name- tag renders the user to a URL which opens in the same window.
<select name="URL" onchange="window.location.href=this.form.URL.options[this.form.URL.selectedIndex].value">
As it currently stands if an individual makes a selection from the list, views their selection (opened in the same window) and returns (to the original webpage), the drop down menu does not revert to:
<option disabled="disabled" selected="selected">Choose a County for Recycling Information.......</option>
but remains on the option value tag selection unless they refresh/reload their browser window.

Basically, you can't do it without javascript.
I don't know how much you know, so I'll assume you have no prior knowledge of javascript.
The following should do what you're asking.
As you can see, I've changed the onchange attribute to call a javascript function called "ResetToDisabled" which passes the DropDown list as a parameter, using the "this" keyword.
<select name="URL" onchange="ResetToDisabled(this);">
<option disabled="disabled" selected="selected">Choose a County for Recycling Information.......</option>
<option value="LINK">Ingham County (Google Map / PDF)</option>
<option value="LINK">Clinton County (PDF)</option>
<option value="LINK">Eaton County (PDF)</option>
</select>
<script type="text/javascript">
function ResetToDisabled(DropDown) {
var TargetIndex = DropDown.selectedIndex;
DropDown.selectedIndex = 0;
window.location.href = DropDown.options[TargetIndex].value;
}
</script>
What is happening here, is that I'm creating a javascript function called "ResetToDisabled" which takes in an object (in this case we can assume it will be a drop down list) as a parameter.
This function, creates a temporary variable called "TargetIndex" and sets it's value to be the selected index of the drop down.
Then it sets the drop down list's selected index to 0 (the first option).
Then it will execute your line of code, where you open a new web page. However since you've reset the drop down back to the first option, when you click back, it will remember that as the option that was selected.
I also feel that I should add. It looks like you're trying to achieve the same behavior that a lot of sites use for their menus; if that's the case, you may want to search "CSS Drop Down Menu" as you'll find it very difficult to change the look of an select drop down if you try to do that later on.

you do not need to use jquery for this!
html file;
<form>
<select name="URL" onchange="changeme(this)">
<option disabled="disabled" selected="selected">Choose a County for Recycling Information.......</option>
<option value="LINK">Ingham County (Google Map / PDF)</option>
<option value="LINK">Clinton County (PDF)</option>
<option value="LINK">Eaton County (PDF)</option>
</select>
</form>
js file;
function changeme(mselect ) {
for (var i = 0; i < mselect.options.length; i++) {
if (mselect.options[i].selected == true) {
mselect.options[i].setAttribute('disabled', 'disabled');
}
else {
mselect.options[i].removeAttribute('disabled');
}
}
}

Depending how they are navigated back to the page, you should be able to just have a little bit of Javascript setting the selected index on page load.
YourHTMLElement.selectedIndex = 0;
I will note however, for it to still have the same value selected it sounds like you are navigating back with history.back(); and this will likely not perform the usual onload events.
I did however find the answer here helpful for such an issue though. It would allow jQuery's document ready event to fire which means you could link it up to reset the selectedIndex to 0.

Related

How to load a md-select based on a different md-select option(binding issue)

Hello i am very new to aurelia js, i have a binding issue that i am stuck with,
I have two md-select drop downs on a page, when the form loads it populates a list of Shops, which works correctly as bellow
<select md-select="label:Shop" value.two-way="addShopProfile.shopId" change.delegate="getItems(addShopProfile.shopId)">
<option value="" disabled selected>Please select</option>
<option repeat.for="shop of shops" value.bind="shop.id">${shop.name}</option>
</select>
You can see i have a change.delegate="getItems(addShopProfile.shopId)"> cause i want the bellow drop down to load the items for a shop based on the shop id
<select md-select="label: Items" if.bind="addShopProfile.shopId" value.two-way="addShopProfile.ItemsId">
<option value="" disabled selected>Please select</option>
<option repeat.for="item of Items" value.bind="item.id">${item.name}</option>
</select>
Now the items load ONLY when i click on the select option to change the Shop(which is correct).
If the shop loads on the form the item for that shop does not show unless i go click on it.
How do i bind the items select option to load instantly when their is a shop already loaded instead of having to click the shop select option to change it on first appearance
What i normally do to get rid of this kind of issue is i call my function (in your case) its getItems(addShopProfile.shopId) inside my attached function that Aurelia provides us. i.e
attached()
{
getItem(id here)
}
So once dom is loaded your select field will get loaded too with data against your provided id.

Angular 2 using LocalStorage to keep selection from dropdown list when page refreshes

I'm trying to get a drop down list to display the selected option when the user refreshes the page. I've got local storage to work to store the value of the current selected option, but I need it to display the selected option after I left the page and came back.
html:
<form #languagesSettings="ngForm">
<div class="form-group row">
<label for="languages">Language</label>
<select (change)="selectLanguage($event)" class="form-control" name="languages" id="languages">
<option selected = "selected" value="en">English</option>
<option value="es">Spanish</option>
<option value="fr">French</option>
<option value="ru">Russian</option>
</select>
</div>
</form>
angular (typescript):
public selectLanguage(event: any): void {
const lang = event.target.value;
localStorage.setItem('language', lang )
}
With all my heart I encourage you to not use local storage for this use case.
Use a service to store the selected index/option in a public field and call that service in your ngOninit to retrieve the values. Run the service as a singleton instance.
This way you will never encounter browser incompatibility problems and will be able to easy distribute stored values across multiple components in the future if need be.
let me know if you need a code example.

keep option selected after a refresh

I have a list of people sorted by 2 parameters that I insert in a select.
How can, when someone select a parameter, maintain this one selected when he refresh or change/return on the page?
I see some issues on some post over the internet, but most of them use JQuery, and I don't want to use it.
The code is like this:
<select name="idactivity_contact[]" id="_activity" multiple="multiple" size="10" style="width:150px;">
<option disabled="disabled" style="background-color:#dddddd;font-weight:bold;">Shipbroking</option>
<option value="1">Newbuilding</option>
<option value="2">Sales and Purchase</option>
<option value="3">Bulk</option>
</select>
The code is generated in PHP actyally manually, but i will do a function later
One way is to use javascript and localStorage:
document.getElementById("_activity").onchange = function() {
localStorage.setItem('selectedtem', document.getElementById("_activity").value);
}
if (localStorage.getItem('item')) {
document.getElementById("selectedtem").options[localStorage.getItem('selectedtem')].selected = true;
}​
You can also use cookies or session in your sever side as well.
Most direct approach would be to store the values of $_POST[] in $_SESSION[].

Make html select option drop down menu open by default

When the select box is clicked, a drop down list of options is displayed. The drop down list stays open until the user clicks outside or selects one of the options in the drop down list.
Is there a simple way of making the drop down list be displayed when user enters page? Like autofocus but better. Similar to how amazon displays it's menu automatically.
I understand that I could probably just make a ul dropdown list + javascript and etc, but was wondering whether there was a way of doing it with select using html or javascript.
you can do it with j query(but i think it is not exactly some thing that you need)
Html Code
<select class="select1" size="5" style="display:none">
<option name="test" value="" class="first">Select</option>
<option name="test" value="" class="">Opt1</option>
<option name="test" value="" class="">Opt2</option>
</select>
Java script
$(document).ready(function() {
$('.select1').toggle();
$(document).click(function(e) {
$('.select1').attr('size',0);
});
});
Demo
You can easily simulate a click on an element, but a click on a <select> won’t open up the dropdown.
Using multiple selects can be problematic. Perhaps you should consider radio buttons inside a container element which you can expand and contract as needed.

using href links inside <option> tag

I have the following HTML code:
<select name="forma">
<option value="Home">Home</option>
<option value="Contact">Contact</option>
<option value="Sitemap">Sitemap</option>
</select>
How can I make Home, Contact and Sitemap values as links?
I used the following code and as I expected it didn't work:
<select name="forma">
<option value="Home">Home</option>
<option value="Contact">Contact</option>
<option value="Sitemap">Sitemap</option>
</select>
UPDATE 2022: This answer is fine but really in 2022 we shouldn't be doing this anymore!
UPDATE (May 2020): Someone asked in the comments why I wouldn't advocate this solution. I guess it's a question of semantics. I'd rather my users navigate using <a> and kept <select> for making form selections because HTML elements have semantic meeting and they have a purpose, anchors take you places, <select> are for picking things from lists.
Consider, if you are viewing a page with a non-traditional browser (a non graphical browser or screen reader or the page is accessed programmatically, or JavaScript is disabled) what then is the "meaning" or the "intent" of this <select> you have used for navigation? It is saying "please pick a page name" and not a lot else, certainly nothing about navigating. The easy response to this is well i know that my users will be using IE or whatever so shrug but this kinda misses the point of semantic importance.
Whereas a funky drop-down UI element made of suitable layout elements (and some js) containing some regular anchors still retains it intent even if the layout element is lost, "these are a bunch of links, select one and we will navigate there".
Here is an article on the misuse and abuse of <select>.
ORIGINAL ANSWER
<select name="forma" onchange="location = this.value;">
<option value="Home.php">Home</option>
<option value="Contact.php">Contact</option>
<option value="Sitemap.php">Sitemap</option>
</select>
UPDATE (Nov 2015): In this day and age if you want to have a drop menu there are plenty of arguably better ways to implement one. This answer is a direct answer to a direct question, but I don't advocate this method for public facing web sites.
You cant use href tags within option tags. You will need javascript to do so.
<select name="formal" onchange="javascript:handleSelect(this)">
<option value="home">Home</option>
<option value="contact">Contact</option>
</select>
<script type="text/javascript">
function handleSelect(elm)
{
window.location = elm.value+".php";
}
</script>
Use a real dropdown menu instead: a list (ul, li) and links. Never misuse form elements as links.
Readers with screen readers usually scan through a automagically generated list of links – the’ll miss these important information. Many keyboard navigation systems (e.g. JAWS, Opera) offer different keyboard shortcuts for links and form elements.
If you still cannot drop the idea of a select don’t use the onchange handler at least. This is a real pain for keyboard users, it makes your third item nearly inaccessible.
The accepted solution looks good, but there is one case it cannot handle:
The "onchange" event will not be triggered when the same option is reselected. So, I came up with the following improvement:
HTML
<select id="sampleSelect" >
<option value="Home.php">Home</option>
<option value="Contact.php">Contact</option>
<option value="Sitemap.php">Sitemap</option>
</select>
jQuery
$("select").click(function() {
var open = $(this).data("isopen");
if(open) {
window.location.href = $(this).val()
}
//set isopen to opposite so next time when use clicked select box
//it wont trigger this event
$(this).data("isopen", !open);
});
(I don't have enough reputation to comment on toscho's answer.)
I have no experience with screen readers and I'm sure your points are valid.
However as far as using a keyboard to manipulate selects, it is trivial to select any option by using the keyboard:
TAB to the control
SPACE to open the select list
UP or DOWN arrows to scroll to the desired list item
ENTER to select the desired item
Only on ENTER does the onchange or (JQuery .change()) event fire.
While I personally would not use a form control for simple menus, there are many web applications that use form controls to change the presentation of the page (eg., sort order.) These can be implemented either by AJAX to load new content into the page, or, in older implementations, by triggering new page loads, which is essentially a page link.
IMHO these are valid uses of a form control.
The <select> tag creates a dropdown list. You can't put html links inside a dropdown.
However, there are JavaScript libraries that provide similar functionality. Here is one example: http://www.dynamicdrive.com/dynamicindex1/dropmenuindex.htm
<select name="career" id="career" onchange="location = this.value;">
<option value="resume" selected> All Applications </option>
<option value="resume&j=14">Seo Expert</option>
<option value="resume&j=21">Project Manager</option>
<option value="resume&j=33">Php Developer</option>
</select>
I'm sure someone can make a better answer than this with Jquery where the change in the dropdown will lead to a new page (good for navigation control on a navbar).
<td><select name="forma" id='SelectOption'>
<option value="Home">Home</option>
<option value="Contact">Contact</option>
<option value="Sitemap">Sitemap</option>
</select></td><td>
document.getElementById('SelectOption').addEventListener('change', function() {
val = $( "#SelectOption" ).val();
console.log(val)
if(val === 'Home') {
window.open('home.php','_blank');
}
if(val === 'Contact') {
window.open('contact.php', '_blank');
}
if (val === 'Sitemap') {
window.open('sitemap.php', '_blank');
}
});