using href links inside <option> tag - html

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');
}
});

Related

How can I put <Input> <datalist> it in the JS file because is increasing weight of HTML?

I have to pass a huge dataset of list of cities/ locations in a particular country to the users because it is going to be reusable.
I can simply put it in the HTML code like so:
<datalist id="cities">
<option value="Mumbai">
<option value="New Delhi">
<option value="Moscow">
<option value="New York">
<option value="Washington">
</datalist>
I feel that each time the user reloads the page, the whole thing makes the HTML file very heavy.
Can I somehow put it in the JS file and pass it to the DOM/HTML without any attached bulky code?
a) Can this be done
b) Is this a good idea in the first place?
So after some fiddling and looking around, I found a solution to the problem myself.
It involves the use of javascript.
To this piece of HTML code:
I added this simple javascript.
<script>
datalist=document.getElementById("countries");
datalist.empty();
datalist.append("<option value="India"><option value="Vietnam"><option value="Russia"><option value="Greece"><option value="New Zealand">")
</script>
And it works.
Now my next aim will be to get this append to be populated by a for next loop. That will make the code even smaller for a large data-list.

How to call HTML button without ID or Name using VBA?

I am not able to search for a solution for this in google. Maybe some will help me here.
Here is the HTML code for the combo box, I need to choose Application Acknowledgement
<select class=”form-control” data-bind=”options: UniqueCorrespondenceTemplates,value: SelectedLetter, optionsCaption:’-- select -- ‘”>
<option value=””>-- select --</option>
<option value=”Application Acknowledgement”>Application Acknowledgement</option>
<option value=”Pend”>Pend</option>
<option value=”Withdraw”>Withdraw</option> </select>
after that, two buttons will appear, Validate and Cancel..
Here's the validate button
<button class=”btn btn-success” data-bind=”click:validation,
visible: $root.canShowValidate() && !$root.ShowConfirmation()”>Validate</button>
I tried looking for the classname, but I does not appear in the loop. Maybe I did wrong. the code I used is in the office. I also tried parent.exep, the javascript trigger, gives a error. I found at google that this is a knockout js, but I don't know what that means.
Can anyone help me call/trigger those dropdown menu and button?

Upgrading Dropdown Menus

I am a teacher, and I currently run a blog via blogger.com for my class. The link is http://stmlatin2019.blogspot.com
You'll notice that I have some drop-down menus in the left sidebar of the blog. These work beautifully on my laptop and on my desktop. The problem is that my school is now switching to iPads, and I am having trouble getting the menus to work on most iPad browsers. They open in Chrome, but the security restrictions on campus distort the look of the blog on Chrome.
My question is this: While I know this is not an Apple forum, could anyone show me how to tweak this code so that the dropdown menus work in iPad browsers like Safari? Currently, Safari lets me pull down the menu and make a selection, but then nothing happens once I make a selection.
I guess I should say that I know absolutely nothing about coding. In fact, I stumbled upon that dropdown code on accident a couple of years ago, and, miraculously, I got it to work. Now I'm back to square one.
Here is the code for one of the menus so that you can see what I'm dealing with:
<select style="background-color:#eee9dd"
id="Select1" onchange="window.open(this.options[this.selectedIndex].value,'_blank');this.options [0].selected=true" class="text_noresize" name="select">
<option selected />Select a link
<option value="" />
<option value="http://www.stmsaints.com/site1.php" /> StM Homepage
<option value="https://mail.stmsaints.com/owa" /> StM E-mail
<option value="http://www.edmodo.com" /> Edmodo
<option value="" /> ____________________</select>"
Any help you can offer me would be most appreciated.
Here is the answer, from Apple docs
The standard window.open() JavaScript method cannot be used to open a new tab and window from a global HTML file or an extension bar. Instead, the global file and extension bars have access to the SafariApplication, SafariBrowserWindow, and SafariBrowserTab classes, whose methods and properties allow you to work with windows and tabs.
Also posted here: link
Safari/Chrome have built-in pop-up blockers that stop this from working. The only Javascript that is allowed to open a new window in Safari/Chrome is Javascript directly attached to click handlers (and other direct user input handlers).
However, you could display an alert showing pop-up blocked.
Try this:
<select id="retailer" class="windowOpen">
<option value="#">Select one</option>
<option value="http://amazon.com">Amazon</option>
<option value="http://ebay.com">eBay</option>
</select>
<script>
$(document).ready(function () {
$('select.windowOpen').change(function () {
var url = $(this).val();
var open = window.open(url);
if (open == null || typeof (open) == 'undefined') alert("Turn off your pop-up blocker!\n\nWe try to open the following url:\n" + url);
});
});
</script>
JSFiddle:
http://jsfiddle.net/utcB9/1

HTML drop down list

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.

Why won't this select open up?

I have a very simple select drop down. In Chrome, it doesn't drop down. The code itself works fine, and the drop down works in Safari, but for some reason it won't open in Chrome. Here is the HTML:
<select name="pellet_credit" class="item_discount">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
It should be pretty simple. It's a dropdown... Here's a screenshot of the select, selected, but not open:
--- edit ---
This is a jsfiddle with the full source included. The dropdown works for me in the jsfiddle view, but not on the actual site.
http://jsfiddle.net/HSYvf/
--- edit ---
Other drop downs on the page work fine.
Validate your HTML to make sure there aren't extraneous closing/end tags. Make sure you aren't hiding the options through CSS as well.
I had the same problem with Firefox and Chrome and due to the z-index of the form being -1.
When changed the z-index, it worked fine.
This happened to me when I put a <select> inside a jQuery .sortable() element. I copied this code right off the jQuery website, and the .disableSelection() method call killed my <select>.
$(function() {
$( "#sortable" ).sortable();
$( "#sortable" ).disableSelection();
});
Once I removed the .disableSelection(); (which oddly enough they've deprecated...) everything worked just fine.
I think you should set a value for your options
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
you can read more here
what ended up happening to me that caused me to be on this page is that display was set to display:none; on the option elements
solution:
$(yourdropdown).children().show();
We had a crazy problem when we were developing a client/server programming language which had a listbox. Although INPUT worked the listbox didn't. We have mouse tracking on it and by a bug the $(window).mousedown... was being enabled by default.
We were able to track the problem with this page: https://hackernoon.com/finding-that-pesky-listener-thats-hijacking-your-event-javascript-b590593f2a83
Just in case the above page disappears:
In Chrome (possibly other Chromium flavours [works on Opera too]):
Right click on element.
Click 'Inspect...'
When the 'Elements' are shown, the right panel will have [Styles][Computed][Event Listeners] (tabs). Click on 'Event Listeners'.
Look for 'mouseup', 'mousedown', 'keyup', etc and expand what you suspect and remove it to see if that fixes the problem (debug).
Change the code.
What we did was change the 'return false' to 'return true' in our code.
To debug such issues try removing all attributes from the html and add them one at a time to find out what is causing the issue.
For example, the snippet below does not work as needed.
<select size="10">
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
Removing the size attribute fixes it
<select >
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
I'm adding an answer just to call out the comment by #Agos in the selected answer. Check if you have event handling code (mousedown, click, etc.) that might be stealing the events from the dropdown.
The problem for me was that I had included class names in the id declaration.
For future audience, notice in particular if your select element is inconsistent with the surrounding form styles. This is a likely clue that a class isn't being applied correctly, and you may have accidentally placed it in the wrong spot.