How to use html select as a navigation - html

how do you use the select tag in html as a navigation?
<select>
<a><option></option></a>
</select
I have tried putting the a tag outside the option tag but it still won't work
Thank you in advance!

Hi #Ivan,
You cannot use the tag inside a element in HTML.
You can use switch statement in JavaScript with window.location = "your link" to achieve that kind of functionality
First create your selection tag with the ID and add event on that tag like this <select name="cars" id="cars" onChange="SelectRedirect()"></select>
now use JavaScript to achieve that. I'm using switch statement.
function SelectRedirect() {
switch (document.getElementById("cars").value) {
case "volvo":
window.location = "page";
break;
case "saab":
window.location = "login";
break;
default:
window.location = "../";
break;
}
}
<div>
<label for="cars">Choose a car:</label>
<select name="cars" id="cars" onChange="SelectRedirect()">
<option value="volvo">volvo</option>
<option value="saab">Saab</option>
</select>
</div>
The another way you can do that with onchange event.
<select onchange="window.location = this.value;">
<option value="">Your page</option>
<option value="page1.html">Page 1</option>
<option value="page2.html">Page 2</option>
<option value="page3.html">Page 3</option>
</select>

Related

Deleting / adding an attribute leads to deleting all <options> in <select>

I try to remove / add the required attribute when I click on the checkbox. I have a problem with this script below:
$(document).on("click", "#billing_as_shipping", function() {
if ($("#billing_as_shipping").is(":checked")) {
$("#wrap-shipping-adress .addRequired").each(function() {
$(this).html($(this).prop('required', false));
});
} else {
$("#wrap-shipping-adress .addRequired").each(function() {
$(this).html($(this).prop('required', true));
});
}
});
<input type="checkbox" id="billing_as_shipping" name="billing_as_shipping">
<div id="wrap-shipping-adress">
<select id="state" name="state" class="form-select addRequired" required>
<option value="">Choose country</option>
<option value="1" selected>Czech Republic</option>
<option value="2">Slovakia</option>
<option value="85">Algeria</option>
<option value="94">Argentina</option>
<option value="68">Armenia</option>
<option value="5">Australia</option>
<option value="33">Austria</option>
<option value="56">Azerbaijan</option>
<option value="45">Belarus</option>
<option value="6">Belgium</option>
<option value="91">Bolivia</option>
</select>
</div>
When I run the script it remove/ add atribute "required", but in case element <select> it delete all <options>. I tried to use it in each function :not, but it didn't work and all <options> disappeared.
With .html(), you are replacing whole html inside select tag, so .html() is not required. You can handle solution without .each() also. Better solution is:
$(document).on("click","#billing_as_shipping", function(){
if($("#billing_as_shipping").is(":checked")){
$("#wrap-shipping-adress .addRequired").prop('required',false);
}else{
$("#wrap-shipping-adress .addRequired").prop('required',true);
}
});
You don't need to call .html() just write:
$(this).prop('required',true))

Html searchable list that redirects to a website depending on the selected option

Is it possible to make the options redirect to a website url?
<input list="browsers">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
Basically how would i combine the code above and the code below?
Google
Thanks
https://jsfiddle.net/kokb207o/
You can use Javascript to accomplish this feat, by changing the href of your element as follows:
HTML
<select id="browsers" onblur='changeHref()'>
<option value="google">Google</option>
<option value="youtube">Youtube</option>
<option value="facebook">Facebook</option>
<option value="instagram">Instagram</option>
<option value="twitter">Twitter</option>
</select>
<a href='javascript: void(0)' id='url' target='_blank'>Click Me!</a>
Javascript
function changeHref() {
document.getElementById('url').href = 'http://www.' + document.getElementById('browsers').value + '.com';
}
Try this snippet. For some reason websites like google.com is not redirecting due to security reasons. But first link working
function redirect(goto) {
if (goto != '') {
window.location = goto;
}
}
var selectEl = document.getElementById('redirectSelect');
selectEl.onchange = function() {
var goto = this.value;
redirect(goto);
};
<select id="redirectSelect">
<option value="">Please select</option>
<option value="https://www.codeconquest.com/what-is-coding/
">Code Conquest</option>
<option value="https://www.google.com">Google</option>
</select>

Load a link after <option> has been selected without submitting

I have a form with two options and whenever the user selects the option I want it to go to a URL. Can this be done without submitting the form? If not, could you help me with the submit part?
Here's my code:
<select>
<option value="Webclient" name="">Web Client</a></option>
<option value="DownloadClient" name="">Download Client</option>
</select>
<select name="forma" ONCHANGE="location = this.options[this.selectedIndex].value;">
<option value="Home.php">Home</option>
<option value="Contact.php">Contact</option>
<option value="Sitemap.php">Sitemap</option>
</select>
<select id="options" onchange="optionCheck()">
<option></option>
<option value="Webclient" name="">Web Client</a></option>
<option value="DownloadClient" name="">Download Client</option>
</select>
<script type="text/javascript">
function optionCheck(){
var option = document.getElementById("options").value;
if(option == "Webclient"){
window.location = "http://yahoo.com";
}
if(option == "DownloadClient"){
window.location = "http://google.com";
}
}
</script>

Default text which won't be shown in drop-down list

I have a select which initially shows Select language until the user selects a language. When the user opens the select, I don't want it to show a Select language option, because it's not an actual option.
How can I achieve this?
Kyle's solution worked perfectly fine for me so I made my research in order to avoid any Js and CSS, but just sticking with HTML.
Adding a value of selected to the item we want to appear as a header forces it to show in the first place as a placeholder.
Something like:
<option selected disabled>Choose here</option>
The complete markup should be along these lines:
<select>
<option selected disabled>Choose here</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>
You can take a look at this fiddle, and here's the result:
If you do not want the sort of placeholder text to appear listed in the options once a user clicks on the select box just add the hidden attribute like so:
<select>
<option selected disabled hidden>Choose here</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>
Check the fiddle here and the screenshot below.
Here is the solution:
<select>
<option style="display:none;" selected>Select language</option>
<option>Option 1</option>
<option>Option 2</option>
</select>
The proper and semantic way is using a placeholder label option:
Add an option element as the first child of the select
Set value= "" to that option
Set the placeholder text as the content of that option
Add the required attribute to the select
This will force the user to select another option in order to be able to submit the form, and browsers should render the option as desired:
If a select element contains a placeholder label option, the user
agent is expected to render that option in a manner that conveys that
it is a label, rather than a valid option of the control.
However, most browsers will render it as a normal option. So we will have to do fix it manually, by adding the following to the option:
The selected attribute, to make it selected by default
The disabled attribute, to make it non-selectable by the user
display: none, to hide it from the list of values
select > .placeholder {
display: none;
}
<select required>
<option class="placeholder" selected disabled value="">Select language</option>
<option>Option 1</option>
<option>Option 2</option>
</select>
Because you can't use assign placeholders for select tags, I don't believe that there is any way to do exactly what you're asking for with pure HTML/CSS. You can, however, do something like this:
<select>
<option disabled="disabled">Select language</option>
<option>Option 1</option>
</select>
"Select language" will show up in the dropdown, but once another option is selected it will not be possible to reselect it.
I hope that helps.
Try this:
<div class="selectSelection">
<select>
<option>Do not display</option>
<option>1</option>
<option>1</option>
</select>
</div>
In the CSS:
.selectSelection option:first-child{
display:none;
}
I have a solution with a span displayed above the select until a selection done. The span displays the default message, and so it's not in the list of propositions:
HTML:
<span id="default_message_overlay">Default message</span>
<select id="my_select">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
CSS:
#default_message_overlay {
position: absolute;
display: block;
width: 120px;
color: grey;
}
select {
width: 150px;
}
Javascript (with JQuery):
$(document).ready(function() {
// No selection at start
$('#my_select').prop("selectedIndex", -1);
// Set the position of the overlay
var offset = $('#my_select').offset();
offset.top += 3;
offset.left += 3;
$('#default_message_overlay').offset(offset);
// Remove the overlay when selection changes
$('#my_select').change(function() {
if ($(this).prop("selectedIndex") != -1) {
$('#default_message_overlay').hide();
}
});
});
I've made a jsfiddle for demo. Tested with Firefox and IE8.
To answer your question directly use this code on the option you do not want it to appear in option list:
<option value="" hidden selected>Select Language</option>
<option value="" id="ddl" name="prop" style="display:none;" disabled selected>chose something </option>
you can of course move the css to a css file if you want, and put a script to catch the esc button to select the disabled again. Unlike the other similar answers I put value="", this is so if you send the value(s) of your select list with a form, it won't contain "chose something". In asp.net mvc 5 sent as json compiled with var obj = { prop:$('#ddl').val(),...}; and JSON.stringify(obj); the value of prop will be null.
Op1:
$("#MySelectid option").each(function () {
if ($(this).html() == "text to find") {
$(this).attr("selected", "selected");
return;
}
});
Op2:
$('#MySelectid option')
.filter(function() { return $.trim( $(this).text() ) == 'text to find'; })​​​​​​​​.attr('selected','selected');​​​​​​​

How can I set the default value for an HTML <select> element?

I thought that adding a "value" attribute set on the <select> element below would cause the <option> containing my provided "value" to be selected by default:
<select name="hall" id="hall" value="3">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
However, this did not work as I had expected. How can I set which <option> element is selected by default?
Set selected="selected" for the option you want to be the default.
<option selected="selected">
3
</option>
In case you want to have a default text as a sort of placeholder/hint but not considered a valid value (something like "complete here", "select your nation" ecc.) you can do something like this:
<select>
<option value="" selected disabled hidden>Choose here</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>
Complete example:
<select name="hall" id="hall">
<option>1</option>
<option>2</option>
<option selected>3</option>
<option>4</option>
<option>5</option>
</select>
I came across this question, but the accepted and highly upvoted answer didn't work for me. It turns out that if you are using React, then setting selected doesn't work.
Instead you have to set a value in the <select> tag directly as shown below:
<select value="B">
<option value="A">Apple</option>
<option value="B">Banana</option>
<option value="C">Cranberry</option>
</select>
Read more about why here on the React page.
You can do it like this:
<select name="hall" id="hall">
<option> 1 </option>
<option> 2 </option>
<option selected> 3 </option>
<option> 4 </option>
<option> 5 </option>
</select>
Provide "selected" keyword inside the option tag, which you want to appear by default in your drop down list.
Or you can also provide attribute to the option tag i.e.
<option selected="selected">3</option>
if you want to use the values from a Form and keep it dynamic try this with php
<form action="../<SamePage>/" method="post">
<?php
$selected = $_POST['select'];
?>
<select name="select" size="1">
<option <?php if($selected == '1'){echo("selected");}?>>1</option>
<option <?php if($selected == '2'){echo("selected");}?>>2</option>
</select>
</form>
Best way in my opinion:
<select>
<option value="" selected="selected" hidden="hidden">Choose here</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>
Why not disabled?
When you use disabled attribute together with <button type="reset">Reset</button> value is not reset to original placeholder. Instead browser choose first not disabled option which may cause user mistakes.
Default empty value
Every production form has validation, then empty value should not be a problem. This way we may have empty not required select.
XHTML syntax attributes
selected="selected" syntax is the only way to be compatible with both XHTML and HTML 5. It is correct XML syntax and some editors may be happy about this. It is more backward compatible. If XML compliance is important you should follow the full syntax.
I prefer this:
<select>
<option selected hidden>Choose here</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>
'Choose here' disappears after an option has been selected.
An improvement for nobita's answer. Also you can improve the visual view of the drop down list, by hiding the element 'Choose here'.
<select>
<option selected disabled hidden>Choose here</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>
Another example; using JavaScript to set a selected option.
(You could use this example to for loop an array of values into a drop down component)
<select id="yourDropDownElementId"><select/>
// Get the select element
var select = document.getElementById("yourDropDownElementId");
// Create a new option element
var el = document.createElement("option");
// Add our value to the option
el.textContent = "Example Value";
el.value = "Example Value";
// Set the option to selected
el.selected = true;
// Add the new option element to the select element
select.appendChild(el);
The selected attribute is a boolean attribute.
When present, it specifies that an option should be pre-selected when the page loads.
The pre-selected option will be displayed first in the drop-down list.
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="vw">VW</option>
<option value="audi" selected>Audi</option>
</select>
If you are in react you can use defaultValue as attribute instead of value in the select tag.
If you are using select with angular 1, then you need to use ng-init, otherwise, second option will not be selected since, ng-model overrides the defaul selected value
<select ng-model="sortVar" ng-init='sortVar="stargazers_count"'>
<option value="name">Name</option>
<option selected="selected" value="stargazers_count">Stars</option>
<option value="language">Language</option>
</select>
I would just simply make the first select option value the default and just hide that value in the dropdown with HTML5's new "hidden" feature. Like this:
<select name="" id="">
<option hidden value="default">Select An Option</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
</select>
value attribute of tag is missing, so it doesn't show as u desired selected. By default first option show on dropdown page load, if value attribute is set on tag.... I got solved my problem this way
This example has been tested to work with multiple <select> elements on the page, and can also work with normal text elements. It has not been tested for setting the value to more than one selection when <select multiple="true">, however you can probably modify this sample to support that.
Add an attribute data-selected to each <select> element and set the value(s) to the value of the option you wish to have selected.
Use javascript's querySelectorAll() to select all elements that have the custom attribute you just added.
In the following example, when run, the first <select> should show option with the value user as selected, and the second <select> should show the option with the value admin as selected.
document.querySelectorAll('[data-selected]').forEach(e => {
e.value = e.dataset.selected
});
<select data-selected="user" class="form-control" name="role">
<option value="public">
Pubblica
</option>
<option value="user">
Utenti
</option>
<option value="admin">
Admin
</option>
</select>
<select data-selected="admin" class="form-control" name="role2">
<option value="public">
Pubblica
</option>
<option value="user">
Utenti
</option>
<option value="admin">
Admin
</option>
</select>
I used this php function to generate the options, and insert it into my HTML
<?php
# code to output a set of options for a numeric drop down list
# parameters: (start, end, step, format, default)
function numericoptions($start, $end, $step, $formatstring, $default)
{
$retstring = "";
for($i = $start; $i <= $end; $i = $i + $step)
{
$retstring = $retstring . '<OPTION ';
$retstring = $retstring . 'value="' . sprintf($formatstring,$i) . '"';
if($default == $i)
{
$retstring = $retstring . ' selected="selected"';
}
$retstring = $retstring . '>' . sprintf($formatstring,$i) . '</OPTION> ';
}
return $retstring;
}
?>
And then in my webpage code I use it as below;
<select id="endmin" name="endmin">
<?php echo numericoptions(0,55,5,'%02d',$endmin); ?>
</select>
If $endmin is created from a _POST variable every time the page is loaded (and this code is inside a form which posts) then the previously selected value is selected by default.
This code sets the default value for the HTML select element with PHP.
<select name="hall" id="hall">
<?php
$default = 3;
$nr = 1;
while($nr < 10){
if($nr == $default){
echo "<option selected=\"selected\">". $nr ."</option>";
}
else{
echo "<option>". $nr ."</option>";
}
$nr++;
}
?>
</select>
You can use:
<option value="someValue" selected>Some Value</option>
instead of,
<option value="someValue" selected = "selected">Some Value</option>
both are equally correct.
Set selected="selected" where is option value is 3
please see below example
<option selected="selected" value="3" >3</option>
I myself use it
<select selected=''>
<option value=''></option>
<option value='1'>ccc</option>
<option value='2'>xxx</option>
<option value='3'>zzz</option>
<option value='4'>aaa</option>
<option value='5'>qqq</option>
<option value='6'>wwww</option>
</select>
You just need to put attribute "selected" on a particular option instead direct to select element.
Here is snippet for same and multiple working example with different values.
Select Option 3 :-
<select name="hall" id="hall">
<option>1</option>
<option>2</option>
<option selected="selected">3</option>
<option>4</option>
<option>5</option>
</select>
<br/>
<br/>
<br/>
Select Option 5 :-
<select name="hall" id="hall">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option selected="selected">5</option>
</select>
<br/>
<br/>
<br/>
Select Option 2 :-
<select name="hall" id="hall">
<option>1</option>
<option selected="selected">2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
Default selected value is Option-4
<html:select property="status" value="OPTION_4" styleClass="form-control">
<html:option value="">Select</html:option>
<html:option value="OPTION_1" >Option-1</html:option>
<html:option value="OPTION_2" >Option-2</html:option>
<html:option value="OPTION_3" >Option-3</html:option>
<html:option value="OPTION_4" >Option-4</html:option>
<html:option value="OPTION_5" >Option-5</html:option>
</html:select>
You will need an "id" attribute in each option for this solution to work:
<script>
function select_option (id,value_selected) {
var select;
select = document.getElementById(id);
if (select == null) return 0;
var option;
option = select.options.namedItem(value_selected);
if (option == null) return 0;
option.selected = "selected";
return true;
}
</script>
<select name="hall" id="hall">
<option id="1">1</option>
<option id="2">2</option>
<option id="3">3</option>
<option id="4">4</option>
<option id="5">5</option>
</select>
<script>select_option ("hall","3");</script>
The function first tries to find the <select> with the id, then it will search for the value_selected in the <select> options and if it finds it, it will set the selected attribute returning true. False otherwise
The problem with <select> is, it's sometimes disconnected with the state of what's currently rendered and unless something has changed in the option list, no change value is returned. This can be a problem when trying to select the first option from a list. The following code can get the first-option the first-time selected, but onchange="changeFontSize(this)" by its self would not. There are methods described above using a dummy option to force a user to make a change value to pickup the actual first value, such as starting the list with an empty value. Note: onclick would call the function twice, the following code does not, but solves the first-time problem.
<label>Font Size</label>
<select name="fontSize" id="fontSize" onfocus="changeFontSize(this)" onchange="changeFontSize(this)">
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
<option value="extraLarge">Extra large</option>
</select>
<script>
function changeFontSize(x){
body=document.getElementById('body');
if (x.value=="extraLarge") {
body.style.fontSize="25px";
} else {
body.style.fontSize=x.value;
}
}
</script>
I use Angular and i set the default option by
HTML Template
<select #selectConnection [(ngModel)]="selectedVal" class="form-control col-sm-6 " max-width="100px" title="Select"
data-size="10">
<option >test1</option>
<option >test2</option>
</select>
Script:
sselectedVal:any="test1";
You can try like this
<select name="hall" id="hall">
<option>1</option>
<option>2</option>
<option selected="selected">3</option>
<option>4</option>
<option>5</option>
</select>
To set the default using PHP and JavaScript:
State: <select id="State">
<option value="" selected disabled hidden></option>
<option value="Andhra Pradesh">Andhra Pradesh</option>
<option value="Andaman and Nicobar Islands">Andaman and Nicobar Islands</option>
.
.
<option value="West Bengal">West Bengal</option>
</select>
<?php
if(isset($_GET['State'])){
echo <<<heredoc
<script>
document.getElementById("State").querySelector('option[value="{$_GET['State']}"]').selected = true;
</script>
heredoc;
}
?>
This is simple method to make default option selected.
Can be used for multiple selects on an HTML page.
The method:
Find every select
Read the id and value of that select
Make the option selected
Note:
Every select must have ID to avoid conflict
$(document).ready(function() {
// Loop for every select in page
$('select').each(function(index, id) {
// Get the value
var theValue = $(this).attr('value');
// Get the ID
var theID = $(this).attr('id');
// Make option selected
$('select#' + theID + ' option[value=' + theValue + ']').attr('selected', true);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<select id="sport" name="sport" class="autoselect" value="golf">
<option value="basket">Basket Ball</option>
<option value="tennis">Tennis</option>
<option value="golf">Golf</option>
<option value="bowling">Bowling</option>
</select>
<hr>
<select id="tools" name="tools" class="autoselect" value="saw">
<option value="hammer">Hammer</option>
<option value="drill">Drill</option>
<option value="screwdriver">Screwdriver</option>
<option value="saw">Saw</option>
<option value="wrench">Wrench</option>
</select>
I was having some troubles with it because I need some way to choose the option dynamically accordingly to the value that I have in the database. The script bellow works like a charm to me:
<?php
//pick the value of database
$selected_sexo = $query['s_sexo'];
?>
<select name="s_sexo" id="s_sexo" required>
<option <?php if($selected_sexo == 'M'){echo("selected");}?> value="M">M</option>
<option <?php if($selected_sexo == 'F'){echo("selected");}?> value="F">F</option>
</select>