How Disable an option on Datalist - html

how can I disable an option in Datalist?
<input type="hidden" name="Town_Group" id="frmTown_Group">
<input name="Town" id="frmTown" list="frmTown_List">
<datalist name="Town_List" id="frmTown_List">
<option value="London">London</option>
<option value="Zurich">Zurich</option>
<option value="|" disabled=true >Italian</option>
<option value="Turin">Turin</option>
<option value="Milan">Milan</option>
<option value="Rome">Rome</option>
<option value="Naples">Naples</option>
<option value="|" disabled=true >French</option>
<option value="Bordeaux">Bordeaux</option>
<option value="Lion">Lion</option>
<option value="Paris">Paris</option>
</datalist>
I tried with disabled, the effect was that the option has disappeared.
Thanks Giovanni Rossati

In that case you can use readonly attribute like this, I wouldn't recommend this
<input type="hidden" name="Town_Group" id="frmTown_Group" />
<input name="Town" id="frmTown" list="frmTown_List" />
<datalist name="Town_List" id="frmTown_List">
<option value="London">London</option>
<option value="Zurich">Zurich</option>
<option value=" " readonly>Italian</option>
<option value="Turin">Turin</option>
<option value="Milan">Milan</option>
<option value="Rome">Rome</option>
<option value="Naples">Naples</option>
<option value=" " readonly>French</option>
<option value="Bordeaux">Bordeaux</option>
<option value="Lion">Lion</option>
<option value="Paris">Paris</option>
</datalist>
This is the only way because
Datalist vs Select:
I guess this is what you want, but there is no comparison. Datalist
is different, Select is different.
datalist is used for autopopulating results from the list, based on
user input, while select doesn't do any magic, it just shows all
options it has.
so this makes clear that there shouldn't be any preselected value in
datalist (as it is used to auto populate, on user interaction). thus
it cant be readonly
select on other hand is different, it can have a default value, thus
it can be readonly.
And yes what you said: "If you desire readonly or disabled, then use a select tag", yes, according to me its true, coz you cant set anything as selected in datalist, but you can in select.

Related

Select box validation is having problem after adding a required attribute

I got a problem when I'm validating this. I can't understand how do I fix it?
<Label for="religion">Select Your Religion</Label>
<select required class="form-control" id="religion">
<option disabled>Select Your Religion</option>
<option value="hindu">Hindu</option>
<option value="muslim">Muslim</option>
<option value="christian">Christian</option>
<option value="buddhist">Buddhist</option>
<option value="other">Other</option>
</select>
Here is the problem I'm getting on https://validator.w3.org/nu :
The first child option element of a select element with a required attribute, and without a multiple attribute, and without a size attribute whose value is greater than 1, must have either an empty value attribute, or must have no text content. Consider either adding a placeholder option label, or adding a size attribute with a value equal to the number of option elements.
The name attribute is missing for your select option
Also, the select option should have value=""
<Label for="religion">Select Your Religion</Label>
<select required name="religion" class="form-control" id="religion">
<option value="">Select Your Religion</option>
<option value="hindu">Hindu</option>
<option value="muslim">Muslim</option>
<option value="christian">Christian</option>
<option value="buddhist">Buddhist</option>
<option value="other">Other</option>
</select>
Like the validator message says
...must have either an empty value attribute, or must have no text content...
Make sure your option has an empty value, like <option disabled value="">
<Label for="religion">Select Your Religion</Label>
<select required class="form-control" id="religion">
<option disabled value="">Select Your Religion</option>
<option value="hindu">Hindu</option>
<option value="muslim">Muslim</option>
<option value="christian">Christian</option>
<option value="buddhist">Buddhist</option>
<option value="other">Other</option>
</select>

How to prevent user from picking default value from dropdown in css?

I have a form in which I have added "Please Select" option for the drop-down.
The form should work in a way that if users doesn't select values 1, 2-5, 6-15, 16-30 etc then the form should not submit.
In my form, if I select "Please Select" then the form gets submit as it being treated as 1, 2-5, 6-15, 16-30, etc.
The HTML codes for the form generated at run time are:
<select name="input_12" id="input_37_12" class="medium gfield_select" tabindex="5" aria-required="true" aria-invalid="false">
<option value="Please Select" selected="selected">Please Select</option>
<option value="1">1</option>
<option value="2-5">2-5</option>
<option value="6-15">6-15</option>
<option value="16-30">16-30</option>
<option value="31-100">31-100</option>
<option value="101-250">101-250</option>
<option value="251-1000">251-1000</option>
<option value="1001-2500">1001-2500</option>
<option value="2501 +">2501 +</option>
</select>
Problem Statement:
I am wondering what changes I should make in the HTML code above so that "Please Select" option is not treated as other drop-down values (1, 2-5, 6-15, 16-30 etc ) in the HTML.
To force form validation on a select input, you need to use the required attribute on your select form and set the value of the initial option to "".
Note: aria-required="true" works fine as well, especially for browsers that don't yet support HTML5, I just prefer the shorter HTML5 alternative. This also applies to selected="selected" vs selected.
<form>
<select name="input_12" id="input_37_12" class="medium gfield_select" tabindex="5" required aria-invalid="false">
<option value="" selected>Please Select</option>
<option value="1">1</option>
<option value="2-5">2-5</option>
<option value="6-15">6-15</option>
<option value="16-30">16-30</option>
<option value="31-100">31-100</option>
<option value="101-250">101-250</option>
<option value="251-1000">251-1000</option>
<option value="1001-2500">1001-2500</option>
<option value="2501 +">2501 +</option>
</select>
<button type="submit">Submit</button>
</form>
Edit: This can also be done via Javascript.
// Obtain our form via its ID
var form = document.querySelector('form');
// Add a listener to our form to wait for its submission
if (form.addEventListener) {
form.addEventListener("submit", validate, false); //Modern browsers
} else if (form.attachEvent) {
form.attachEvent('onsubmit', validate); //Old IE
}
function validate(e) {
var select = e.target.querySelector("select");
// Get the value of our selected option
var selectedOption = select.options[select.selectedIndex].value;
// Compare the value of the default option to the selected option
if (selectedOption === "Please Select") {
// Trigger Error and prevent the form submission
alert("Please select an option!")
e.preventDefault();
}
}
<form>
<select name="input_12" id="input_37_12" class="medium gfield_select" tabindex="5" aria-required="true" aria-invalid="false">
<option value="Please Select" selected="selected">Please Select</option>
<option value="1">1</option>
<option value="2-5">2-5</option>
<option value="6-15">6-15</option>
<option value="16-30">16-30</option>
<option value="31-100">31-100</option>
<option value="101-250">101-250</option>
<option value="251-1000">251-1000</option>
<option value="1001-2500">1001-2500</option>
<option value="2501 +">2501 +</option>
</select>
<button type="submit">Button</button>
</form>
To simply prevent the users from selecting an option just add disabled in the html for the option (as pointed out in the question from the comment marking this as a possible duplicate, #csmckelvey).
However, you can also simply add a display:none to the option:
select option:first-of-type{
display:none;
}
At least works on android, and yet show the text of the first option (even though it Should be hidden - dont Know why).
Anyways it's important to Remember to validate the form as the other answer suggests.

Is there a way to fill select element with datalist

For input element we can use datalist element as autocomplete
<input name="account" placeholder="account" list="publisher">
<datalist id="publisher">
<option value="00000" >00000</option>
<option value="00001" >00001</option>
<option value="00002">00002</option>
</datalist>
there is a way to do so for select element ? not autocomplete just fill the select with options for example:
the two example should be equivalent
<select name=account>
<option value="00000" >00000</option>
<option value="00001" >00001</option>
<option value="00002">00002</option>
</select>
and that
<select name="account" list="publisher"></select>

HTML select options are inaccessible

I must have missed something very trivial, but I cant figure it out.
The select below shows 5 options (as expected), but I cant get access to option 6 and 7. The scrollbar is shown, but the browser doesnt allow me to move it down
<select size='5' multiple name='driver[]'>
<option value= >(No Driver Filter)</option>
<option value=drivername=''></option>
<option value=drivername='alex'>alex</option>
<option value=drivername='marc'>marc</option>
<option value=drivername='frank'>frank</option>
<option value=drivername='james'>james</option>
<option value=drivername='michael'>michael</option>
</select>
I think your code is not well formatted . You should use double quotes for attribute values.
<select size="5" multiple="multiple" name="driver[]">
<option value="" >(No Driver Filter)</option>
<option value="drivername=''"></option>
<option value="drivername='alex'">alex</option>
<option value="drivername='marc'">marc</option>
<option value="drivername='frank'">frank</option>
<option value="drivername='james'">james</option>
<option value="drivername='michael'">michael</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>