Drop down selection is still selectable with "readonly" attribute - html

I want a read-only "select" element to be not selectable, the same behavior as the readonly input box.
In the code below, you cannot change the value for the input box with the value "abc". However, you can still change the selection in the drop. I can't use "disabled" attribute because I still need to send these values to the server.
<input type="text" readonly="readonly" value="abc">
</input>
<select readonly="readonly">
<option>Item ABC</option>
<option>Item XYZ</option>
</select>
https://jsfiddle.net/6Lu1jpLx/

Simplist way to resolve this would be to use the below line:
$("#MySelect").css("pointer-events","none");
However, the following worked for me where in my case I wanted my Select to still have the disabled cursor - setting 'pointer-events' to 'none' will no longer show cursor as 'not-allowed'.
JQUERY
var isReadOnly = $("#MyCheckbox").prop("checked");
var domElements = $("#MyInput, #MySelect");
$(domElements).prop("readonly", isReadOnly);
$(domElements).toggleClass("my-read-only-class", isReadOnly);
$(domElements).find("option").prop("hidden", isReadOnly);
CSS
.my-read-only-class
{
cursor: not-allowed;
}
JSFiddle https://jsfiddle.net/xdddzupm/

style="pointer-events: none;"
Is worked for me
<select style="pointer-events: none;">
<option>Item ABC</option>
<option>Item XYZ</option>
</select>

$("select :selected").each(function(){
$(this).parent().data("default", this);
});
$("select").change(function(e) {
$($(this).data("default")).prop("selected", true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select disabled>
<option value="foo1">foo1</option>
<option value="bar1" selected="selected">bar1</option>
<option value="test1">test1</option>
</select>
Try below code
<select>
<option value="foo1">foo1</option>
<option value="bar1" selected="selected">bar1</option>
<option value="test1">test1</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))

Setting default value to an HTML dropdown

I have created a dropdown menu in html. I have a reset button which resets all the value to null. But on clicking this I need to reset the dropdown menu to the first value which I gave in the field.
Please help...
The code for the dropdown is as below.
<select name="abc" id="abc">
<option value="one">One
<option value="two">Two
</select>
But on clicking Reset I am getting a blank in the dropdown list from where i have to again select either One or Two.
Instead I want it to automatically set to the value "One".
You can use add the attribute selected to the option you want to be default like this:
<select name="abc" id="abc">
<option value="one">One</option>
<option value="two" selected>Two</option>
</select>
Or add selected="selected":
<select name="abc" id="abc">
<option value="one" selected="selected">One</option>
<option value="two">Two</option>
</select>
You just need to use the selected attribute for the item that you want to be the default:
<select name="abc" id="abc">
<option value="one" selected="selected">One</option>
<option value="two">Two</option>
</select>
If you are manually performing the reset, you'll likely want to store a reference to the current selection prior to resetting them and using that to retain the previous selection, which you can access via the selectedIndex property. Depending on your use case and technology (e.g. jQuery, etc.), you can select a specific element by its index, value, or potentially other attributes.
You can add selected attributes to make default select.
<select name="abc" id="abc">
<option value="one">One
<option value="two" selected="selected">Two
</select>
With jquery
$("#abc").val("two")
use selected
here live example https://www.w3schools.com/tags/att_option_selected.asp
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="vw">VW</option>
<option value="audi" selected>Audi</option>
</select>
I think you need this code in your reset jquery function.
$("#abc option:selected").prop("selected", false);
$("#abc option:first").prop("selected", "selected");
HTML select drop down by default shows first option if none of the option is selected. So, effectively you just need to clear the selected attibute for your dropdown.
function clearForm(form) {
//var $f = $(form);
//var $f = $f.find(':input').not(':button, :submit, :reset, :hidden');
//$('#msg').empty();
$('#abc option:selected').removeAttr('selected');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="abc" id="abc">
<option value="one">One
<option value="two" selected>Two
</select>
<input type='button' class="btn btn-primary" value='Reset' name='reset' onclick="return clearForm(this.form);">

Selected just only one value option from several option

<pre>
<td>
<select name="proses1" class="form-control">
<option value="0" selected>-------</option>
<option value="1">1-Itemset</option>
<option value="2">2-Itemset</option>
<option value="3">3-Itemset</option>
</select>
</td>
<td>
<select name="proses2" class="form-control">
<option value="0" selected>-------</option>
<option value="1">1-Itemset</option>
<option value="2">2-Itemset</option>
<option value="3">3-Itemset</option>
</select>
</td>
<td>
<select name="proses3" class="form-control">
<option value="0" selected>-------</option>
<option value="1">1-Itemset</option>
<option value="2">2-Itemset</option>
<option value="3">3-Itemset</option>
</select>
</td>
</pre>
i have tag html like above.. and i want for example i selected 1-itemset from proses1 and then i choose 1-itemset from proses2,
1-itemset from proses1 it get back to default(-------),
so the point is i want option just can selected one option.. how i can make it. thanks before
Although you havent said what type of process you want, i suggest you do it with jquery like
var selects = $("select[name=proses1],select[name=proses2],select[name=proses3]");
selects.on("change",function(evt){
selects.not("[name=" + evt.target.name + "]").val(0);
});
FIDDLE
EDIT
To use this, first you need to add jQuery library to your code then place this inside $(document).ready() method. As a summary, place the code below inside your head section.
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var selects = $("select[name=proses1],select[name=proses2],select[name=proses3]");
selects.on("change",function(evt){
selects.not("[name=" + evt.target.name + "]").val(0);
});
});
</script>
What this does is to add an onChange event handler with .on("change") method to the select elements and in that handler all select elements values are being set to 0 which is the very first option except the current one.

Bootstrap select dropdown list placeholder

I am trying to make a dropdown list that contains a placeholder. It doesn't seem to support placeholder="stuff" as other forms do. Is there a different way to obtain a placeholder in my dropdown?
Yes just "selected disabled" in the option.
<select>
<option value="" selected disabled>Please select</option>
<option value="">A</option>
<option value="">B</option>
<option value="">C</option>
</select>
Link to fiddle
You can also view the answer at
https://stackoverflow.com/a/5859221/1225125
Use hidden:
<select>
<option hidden >Display but don't show in list</option>
<option> text 1 </option>
<option> text 2 </option>
<option> text 3 </option>
</select>
dropdown or select doesn't have a placeholder because HTML doesn't support it but it's possible to create same effect so it looks the same as other inputs placeholder
$('select').change(function() {
if ($(this).children('option:first-child').is(':selected')) {
$(this).addClass('placeholder');
} else {
$(this).removeClass('placeholder');
}
});
.placeholder{color: grey;}
select option:first-child{color: grey; display: none;}
select option{color: #555;} // bootstrap default color
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="form-control placeholder">
<option value="">Your Placeholder Text</option>
<option value="1">Text 1</option>
<option value="2">Text 2</option>
<option value="3">Text 3</option>
</select>
if you want to see first option in list remove display property from css
Most of the options are problematic for multi-select.
Place Title attribute, and make first option as data-hidden="true"
<select class="selectpicker" title="Some placeholder text...">
<option data-hidden="true"></option>
<option>First</option>
<option>Second</option>
</select>
If you are initializing the select field through javascript, the following can be added to replace the default placeholder text
noneSelectedText: 'Insert Placeholder text'
example: if you have:
<select class='picker'></select>
in your javascript, you initialize the selectpicker like this
$('.picker').selectpicker({noneSelectedText: 'Insert Placeholder text'});
Add hidden attribute:
<select>
<option value="" selected disabled hidden>Please select</option>
<option value="">A</option>
<option value="">B</option>
<option value="">C</option>
</select>
Try this:
<select class="form-control" required>
<option value="" selected hidden>Select...</option>
when using required + value="" then user can not select it
using hidden will make it hidden from the options list, when the user open the options box
Try #title = "stuff". It worked for me.
All this options are.... improvisations.
Bootstrap already offers a solution for this.
If you want to change the placeholder for all your dropdown elements you can change the value of
noneSelectedText
in the bootstrap file.
To change individualy, you can use TITLE parameter.
example:
<div class="form-group">
<label class="control-label col-xs-2" id="country">Continent:</label>
<div class="col-xs-9">
<select name="zones[]" class="selectpicker" title="All continents" id="zones" multiple >
<option value="Africa">Africa</option>
<option value="Asia">Asia</option>
<option value="North America">America North</option>
<option value="South America">America South</option>
<option value="Antarctica">Antarctica</option>
<option value="Australia">Australia</option>
<option value="Europe">Europe</option>
</select>
</div>
</div>
Using Bootstrap, This is what you can do. Using class "text-hide", the disabled option will be shown at first but not on the list.
<select class="form-control" >
<option selected disabled class="text-hide">Title</option>
<option>A</option>
<option>B</option>
<option>C</option>
</select>
in bootstrap-select.js Find title: null, and remove it.
add title="YOUR TEXT" in <select> element.
Fine :)
Example:
<select title="Please Choose one item">
<option value="">A</option>
<option value="">B</option>
<option value="">C</option>
</select>
I think, the Dropdown box with a class and JQuery code to disable the first option for user to select, will work perfectly as Select Box placeholder.
<select class="selectboxclass">
<option value="">- Please Select -</option>
<option value="IN">India</option>
<option value="US">America</option>
</select>
Make the first option disabled by JQuery.
<script>
$('select.selectboxclass option:first').attr('disabled', true);
</script>
This will make the first option of Dropdown as Placeholder and user will no longer able to select the first option.
Hope It helps!!
The right way to achieve what you are looking for is to use the title attribute.
The title global attribute contains text representing advisory information related to the element it belongs to.
title="Choose..."
for example:
<select class="form-control selectpicker" name="example" title="Choose...">
<option value="all">ABCDEFG</option>
<option value="all">EFG</option>
</select>
check the documentation for more info about bootstrap-select
custom-button-text
Here's another way to do it
<select name="GROUPINGS[xxxxxx]" style="width: 60%;" required>
<option value="">Choose Platform</option>
<option value="iOS">iOS</option>
<option value="Android">Android</option>
<option value="Windows">Windows</option>
</select>
"Choose Platform" becomes the placeholder and the 'required' property ensures that the user has to select one of the options.
Very useful, when you don't want to user field names or Labels.
Bootstrap select has an noneSelectedText option. You can set it via data-none-selected-text attribute.
Documentation.
For .html page
<select>
<option value="" selected disabled>Please select</option>
<option value="">A</option>
<option value="">B</option>
<option value="">C</option>
</select>
for .jsp or any other servlet page.
<select>
<option value="" selected="true" disabled="true">Please select</option>
<option value="">A</option>
<option value="">B</option>
<option value="">C</option>
</select>
Using bootstrap, if you need to also add some values to the option to use for filters or other stuff you can simply add the class "bs-title-option" to the option that you want as a placeholder:
<select class="form-group">
<option class="bs-title-option" value="myVal">My PlaceHolder</option>
<option>A</option>
<option>B</option>
<option>c</option>
</select>
Bootstrap adds this class to the title attribute.
Solution for Angular 2
Create a label on top of the select
<label class="hidden-label" for="IsActive"
*ngIf="filterIsActive == undefined">Placeholder text</label>
<select class="form-control form-control-sm" type="text" name="filterIsActive"
[(ngModel)]="filterIsActive" id="IsActive">
<option value="true">true</option>
<option value="false">false</option>
</select>
and apply CSS to place it on top
.hidden-label {
position: absolute;
margin-top: .34rem;
margin-left: .56rem;
font-style: italic;
pointer-events: none;
}
pointer-events: none allows you to display the select when you click on the label, which is hidden when you select an option.
angular html css
<option value="" defaultValue disabled> Something </option>
you can replace defaultValue with selected but that would give warning.
This is for Bootstrap 4.0, you only need to enter selected on the first line, it acts as a placeholder. The values are not necessary, but if you want to add value 0-... that is up to you.
Much simpler than you may think:
<select class="custom-select">
<option selected>Open This</option>
<option value="">1st Choice</option>
<option value="">2nd Choice</option>
</select>
This is link will guide you for further information.

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