Select an option default by giving a value to select - html

Hello is there anyway to have a value selected by giving in a value?
<select name="color">
<option value="black">black</option>
<option value="white">white</option>
<option value="red">red</option>
</select>
For example if I want black to be selected I would set selected = black.
Rather than using <option value="black selected>black</option>
Thank you for assistance, please let me know if there is any misunderstanding.

If you are not averse to using js and jquery:
<select name="color" id="color">
<option value="black">black</option>
<option value="white">white</option>
<option value="red">red</option>
</select>
<script type="text/javascript">
$(document).ready(function () {
$("#color").val("black");
});
</script>

If you want to work with multiple select options, you can use this:
$("#color").val(["black"]);
// for single select option
$("#color").val(["black","red","white"]);
// for multiple select options

Related

Selecting an option in drop down menu one determines list available in the second drop down menu

I want to make the contains of a second drop down menu dependent on what is selected in the the first. In my case wish to do it for a car selection option. i.e when the car make is selected this then populates the model drop down menu with the relevant models.
As there are a lot of makes of car I would like to keep it concise if possible I presume arrays of models for each make is the solution but how do implement this.
I have look at various solutions and cannot find a suitable answer to this, any suggestions/examples would be gratefully appreciated.
I am using jsp.
Make:<select>
<option value="audi">Audi</option>
<option value="bmw">BMW</option>
<option value="ford">Ford</option>
<option value="toyota">Toyota</option>
</select>
You could do it with some simple JS and css ( here is a JSFiddle: http://jsfiddle.net/N7Q57/1/)
Make:
<select name="cars" id="cars">
<option selected value="Choose">Choose</option>
<option value="Audi">Audi</option>
<option value="BMW">BMW</option>
<option value="Ford">Ford</option>
<option value="Toyota">Toyota</option>
</select>
<div id="brands">
<select class="audi">
<option value="1">1</option>
<option value="2">2</option>
</select>
<select class="bmw">
<option value="3">3</option>
<option value="4">4</option>
</select>
</div
the jQuery
$("#cars").change(function () {
var string = "";
string = $("select#cars option:selected").text();
if (string == "Audi"){
$("#brands .bmw").hide();
$("#brands .audi").show();
$("#brands").show();
} else if (string == "BMW"){
$("#brands .audi").hide();
$("#brands .bmw").show();
$("#brands").show();
}
})
and the CSS:
#brands{
display: none;
}

How to show disable HTML select option in by default?

I am new to HTML and PHP and want to achieve a drop-down menu from the mysql table and hard-coded too. I have multiple select in my page, One of them is
<select name="tagging">
<option value="">Choose Tagging</option>
<option value="Option A">Option A</option>
<option value="Option B">Option B</option>
<option value="Option C">Option C</option>
</select>
Problem is now that user can also select "Choose Tagging" as his tagging but i only want to provide him to chose from available three. I used disable as
<select name="tagging">
<option value="" disabled="disabled">Choose Tagging</option>
<option value="Option A">Option A</option>
<option value="Option B">Option B</option>
<option value="Option C">Option C</option>
</select>
But now "Option A" became the default one. So i want to set "Choose Tagging" as by default and also want to disable it from selection. Is it a way to do this. Same thing need to be done with other select which will fetch data from Mysql. Any suggestion will be appreciable.
use
<option selected="true" disabled="disabled">Choose Tagging</option>
Use hidden.
<select>
<option hidden>Choose</option>
<option>Item 1</option>
<option>Item 2</option>
</select>
This doesn't unset it but you can however hide it in the options while it's displayed by default.
In HTML5, to select a disabled option:
<option selected disabled>Choose Tagging</option>
Electron + React.
Let your two first options look like this.
<option hidden="true">Choose Tagging</option>
<option disabled="disabled" default="true">Choose Tagging</option>
First to display when closed.
Second to display first when the list opens.
I know you ask how to disable the option, but I figure the end users visual outcome is the same with this solution, although it is probably marginally less resource demanding.
Use the optgroup tag, like so :
<select name="tagging">
<optgroup label="Choose Tagging">
<option value="Option A">Option A</option>
<option value="Option B">Option B</option>
<option value="Option C">Option C</option>
</optgroup>
</select>
Another SELECT tag solution for those who want to keep first option blank.
<label>Unreal :</label>
<select name="unreal">
<option style="display:none"></option>
<option>Money</option>
<option>Country</option>
<option>God</option>
</select>
<select name="dept" id="dept">
<option value =''disabled selected>Select Department</option>
<option value="Computer">Computer</option>
<option value="electronics">Electronics</option>
<option value="aidt">AIDT</option>
<option value="civil">Civil</option>
</select>
use "SELECTED" which option you want to select by defult.
thanks
selected disabled="true"
Use this. It will work in new browsers
we can disable using this technique.
<select class="form-control" name="option_select">
<option selected="true" disabled="disabled">Select option </option>
<option value="Option A">Option A</option>
<option value="Option B">Option B</option>
<option value="Option C">Option C</option>
</select>
If you are using jQuery to fill your select element, you can use this:
html
<select id="tagging"></select>
js
array_of_options = ['Choose Tagging', 'Option A', 'Option B', 'Option C']
$.each(array_of_options, function(i, item) {
if(i==0) { sel_op = 'selected'; dis_op = 'disabled'; } else { sel_op = ''; dis_op = ''; }
$('<option ' + sel_op + ' ' + dis_op + '/>').val(item).html(item).appendTo('#tagging');
})
This will allow the user to see the first option as a disabled heading ('Choose Tagging'), and select all other options.
this should help....:)
<label for="cars">Choose a car:</label>
<select name="cars" id="cars">
<option value=""selected="true" disabled="disabled"> Select</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
This will hide the option for you this easier that to make a other dropdown box
<option selected="true" disabled="disabled">Choose Tagging</option>
You can set which option is selected by default like this:
<option value="" selected>Choose Tagging</option>
I would suggest using javascript and JQuery to observe for click event and disable the first option after another has been selected:
First, give the element an ID like so:
<select id="option_select" name="tagging">
and the option an id :
<option value="" id="initial">Choose Tagging</option>
then:
<script type="text/javascript">
$('option_select').observe(click, handleClickFunction);
Then you just create the function:
function handleClickFunction () {
if ($('option_select').value !== "option_select")
{
$('initial').disabled=true; }
}

bar rating - wanting default selection to be no selection

From http://netboy.pl/demo/jquery-bar-rating/examples/ - I'm using the first rating system using 10 bars. However, I want the default to be no selected option at all (i.e. no bars). Right now the default selection is option #1 - any thoughts? I did try adding an empty option but all it did was add another bar to the front (for a total of 11 bars which is not what I want). I want the 10 bars but none highlighted/selected. Any thoughts?!?
<div class="input select rating-a">
<select class="example-a" name="content_rating">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
Since you're using jquery you can do something like this:
$('.example-a').prop("selectedIndex", -1);
http://jsfiddle.net/GAXBf/
URL : http://netboy.pl/demo/jquery-bar-rating/examples/ (This url is not working)
If i am not wrong, you are using jquery bar rating like (http://antenna.io/demo/jquery-bar-rating/examples/).
In this jquery bar rating library, we can set no value selected by default on bar.
<script>
$('#rating').barrating('show', {
theme: 'bars-1to10',
deselectable: true
});
</script>
HTML:
<select id="rating">
<option value=""></option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
.
.
<option value="10">10</option>
</select>
Hope, It will resolve your problem.
In case its still relevant. You can add a empty Option.
<select id="rating">
<option value></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
This will give 5 stars with zero filled. Off course this will only work if there is no initial value defined.
<select id="example">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
Just in case anyone was wondering, in the latest version of the jQuery Bar Rating Plugin (v1.0.5) you can add the following option to do the trick:
<option disabled selected>Select an option</option>
So your select box will look something like this:
<select>
<option disabled selected>Select an option</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<!-- etc. -->
</select>
I was having the same issue as the original poster with jQuery Bar Rating (http://antenna.io/demo/jquery-bar-rating/examples/)
When called each option within the select field was transformed to a link wrapped in a div with a class of "br-widget"
I only wanted to display 5 stars for the user to choose from, however the amount of options in the select would determine how many star ratings would show on the screen. The solution I found was to add a "Please Select" as an option and then to hide this using jquery as shown below.
$(document).ready(function() {
$(\'#rating\').barrating({
theme: \'css-stars\'
});
//HIDE THE FIRST STAR OPTIONS
$(".br-widget a:first-child").css( "display", "none" );
});
<select name="rating" id="rating">
<option value="0">Please Select</option>
<option value="1">1 Star</option>
<option value="2">2 Stars</option>
<option value="3">3 Stars</option>
<option value="4">4 Stars</option>
<option value="5">5 Stars</option>
</select>
I had the same problem with this latest 2016 version and i solved it by adding this javascript AFTER the function call and now everything works (hope this help someone):
$(document).ready(function() {
// remove the selection on the dropdown menu
$("#rating").val([]);
// remove all "selected" css to show as "unselected" by default
$(".br-widget a").removeClass('br-selected');
// remove default "selected text", if any
$(".br-current-rating").empty();
});
Here is my full code:
<script>
// 5-star rating script
$(function() {
$('#rating').barrating({
theme: 'fontawesome-stars',
initialRating: null, // initial rating
showValues: false, // display rating values on the bars?
showSelectedRating: true, // append a div with a rating to the widget?
deselectable: true, // allow to deselect ratings
reverse: false, // reverse the rating?
readonly: false, // make the rating ready-only?
fastClicks: true, // remove 300ms click delay on touch devices?
hoverState: true, // change state on hover?
silent: false, // supress callbacks when controlling ratings programatically
});
});
$(document).ready(function() {
// remove the selection on the dropdown menu
$("#rating").val([]);
// remove all "selectec" css to show as "unselected" by default
$(".br-widget a").removeClass('br-selected');
// remove default "selected text", if any
$(".br-current-rating").empty();
});
</script>
<style>
.br-wrapper{
width: auto;
display: inline-block;
}
.br-widget{
width: auto;
}
</style>

Programmatically choose item in select drop down

I have a drop down:
<select>
<option value="1">1</option>
<option value="2">2</option>
</select>
How would I select item 2 programmatically?
First get a handle on that select somehow:
var select = document.getElementsByTagName("SELECT")[0];
Then manipulate the selectedIndex property (I believe it's a zero-based index):
select.selectedIndex = 1;
If you're talking about pre-selecting an item, simply set the item as "selected" as follows:
<select>
<option value="1">1</option>
<option value="2" selected="selected">2</option>
</select>
If you're using plain HTML:
<option selected value="2">2</option>
If you're able to use jQuery, use the val() method:
<select id="foo"> //give it an id
$("#foo").val("2");
jQuery edition...
$('#your_select').val('2');
<select>
<option value="1">1</option>
<option value="2" selected>2</option>
</select>
The easiest solution is this:
<select>
<option value="1">1</option>
<option selected value="2">2</option>
</select>

HTML select dropdown list

I want to have a drop down list using the select, option tag... but when it first appear I want it to have an information, such as "Please select a name" then the user clicks on the drop down list and selects from the available option... I tried to made the "Please select a name" as an option, but then the user will be able to select this... which is not what I want. Do I need to use javascript to have this feature or what do I need to do?
If there'a jquery way to do this, this would be much helpful
Try this:
<select>
<option value="" disabled="disabled" selected="selected">Please select a name</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>
When the page loads, this option will be selected by default. However, as soon as the drop-down is clicked, the user won't be able to re-select this option.
<select>
<option value="" style="display:none">Choose one provider</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>
This way the user cannot see this option, but it shows in the select box.
Have <option value="">- Please select a name -</option> as the first option and use JavaScript (and backend validation) to ensure the user has selected something other than an empty value.
This is an old post, but this worked for me
<select>
<option value="" disabled selected>Please select a name...</option>
<option>this</option>
<option>that</option>
</select>
<select name="test">
<option hidden="true">Please select a name</option>
<option value="Cash">Cash</option>
<option value="Draft">Demand Draft No.</option>
<option value="Cheque">Cheque No.</option>
</select>
Maybe this can help you resolve without JavaScript
http://htmlhelp.com/reference/html40/forms/option.html
See DISABLE option
Simple, I suppose. The onclick attribute works nicely...
<select onchange="if(this.value == '') this.selectedIndex = 1; ">
<option value="">Select an Option</option>
<option value="one">Option 1</option>
<option value="two">Option 2</option>
</select>
After a different option is selected, if the first option is selected, Option 1 will be selected. If you want an explanation on the javascript, just ask.
<select>
<option value="" disabled="disabled" selected="selected">Please select name</option>
<option value="Tom">Tom</option>
<option value="Marry">Marry</option>
<option value="Jane">Jane</option>
<option value="Harry">Harry</option>
</select>
If you want to achieve the same for the jquery-ui selectmenu control then you have to set 'display: none' in the open event handler and add '-menu' to the id string.
<select id="myid">
<option value="" disabled="disabled" selected="selected">Please select name</option>
<option value="Tom">Tom</option>
<option value="Marry">Mary</option>
<option value="Jane">Jane</option>
<option value="Harry">Harry</option>
</select>
$('select#listsTypeSelect').selectmenu({
change: function( event, data ) {
alert($(this).val());
},
open: function( event, ui ) {
$('ul#myid-menu li:first-child').css('display', 'none');
}
});
I'm sorry to necro an old post - but I found a better way of doing this
What I believe this poster wanted was :
<label for="mydropdown" datalabel="mydropdown">Country:</label>
<select name="mydropdown">
<option value="United States">United States</option>
<option value="Canada">Canada</option>
<option value="Mexico">Mexico</option>
<option value="Other">Not Listed</option>
</select>
I found this information in another post while searching for this same answer - Thanks
<select>
<option value="" disabled selected hidden>Select an Option</option>
<option value="one">Option 1</option>
<option value="two">Option 2</option>
</select>
Make a JavaScript control that before the submit cheek that the selected option is different to your first option
This is how I do this with JQuery...
using the jquery-watermark plugin (http://code.google.com/p/jquery-watermark/)
$('#inputId').watermark('Please select a name');
works like a charm!!
There is some good documentation at that google code site.
Hope this helps!
<select>
<option value="" disabled="disabled" selected="selected">Please select a
developer position</option>
<option value="1">Beginner</option>
<option value="2">Expert</option>
</select>
From what I understand, you are looking for a placeholder for your select options, which has to be selected when no value is pre-selected, and cannot be selected by user, or seen.
<select name="selectOne">
<option value="" disabled selected hidden>Choose an option</option>
<option value="this">This</option>
<option value="that">That</option>
</select>