HTML Form: List - Number of elements shown - html

In my html form, I currently have a text input as follows:
<input list="BMUnits" name"BMUnitID" value"<?php echo $chosenBMU ?>" autocomplete="off">
<datalist id="BMUnits">
<option value="1">
...
<option value="300">
</datalist>
When the user types the drop down that shows the datalist goes off the screen, is there a way to define a certain number of elements in the datalist to be shown at once?

It seems that it is not possible. The current <datalist> tag does not support any special HTML attribute nor you can use CSS to style the dropped list. This means that only the browser can decide how the dropped list will look like. For example, IE (now called MS Edge) will show a scrollbar if the list is too long, but chrome won't. All we can do is waiting until browsers support that.
Alternativly, you can use jQueryUI Combobox or Chosen for more flexible drop list.

Related

Input text Field inside `<select>` tag

I am creating a dropdown with AngularJS.
here is the code..
<div class="col-md-9">
<input type="text" placeholder="Enter Module" list="names"
ng-model="data.Name" />
<select id="names" class="form-control" ng-model="data.Name"
ng-change="SetCategory(data.Name)" name="name">
<option value='' disabled selected>------------- select an option ----------</option>
<option ng-repeat="e in BrData | filter:data.Name "
value="{{e.Name}}">{{e.Price}}</option>
</select>
</div>
NOTE: List is Dynamic and i am using AngularJS to get data.
I need To create a searchbar inside select tag.
But Input tag can't nested in select tag.What should I do?
You can use typeahead from UI Bootstrap: https://angular-ui.github.io/bootstrap/#!#typeahead
Or if you need more advanced features along with search like multi-select, select all, deselect all, disable options, keyboard controls and much more try this: http://dotansimha.github.io/angularjs-dropdown-multiselect/docs/#/main
The way I see it, there are three options here.
Option One - Input outside the dropdown
Get the input outside the dropdown, and filter the values based on that value from the outside. I know that this is not your intended functionality exactly, but it would save you some trouble.
Option Two - Use some kind of third party dropdown library
As Mohd mentioned https://angular-ui.github.io/bootstrap/#!typeahead is a good fit and UI select too
Option three - Create something of your own
It need not even be using <select> tag. This is by far the most difficult, but also the most customizable and suitable for individual needs. The select tag will not be used as it does not support input inside of it, so some high end css will need to be used, as well as some backwards compatibility multiple browser testing that the already made libraries have already done.
Dealing with the <select> nightmare
From the Docs:
<select> Element Technical summary1
Permitted content: Zero or more <option> or <optgroup> elements.
Tag omission: None, both the starting and ending tag are mandatory.
Permitted parents: any element that accepts phrasing content
The short answer is that <input> elements can not be placed inside <select> elements.
<datalist> Element2
The datalist element is intended to provide a better mechanism for this concept.
<input type="text" name="example" list="exampleList">
<datalist id="exampleList">
<option value="A">
<option value="B">
</datalist>
For more information, see
HTML combo box with option to type an entry
MDN Learn HTML Forms - Dealing with the select nightmare
Use select2 as a dynamic option instead of HTML select option:
here is link for select using js:
https://select2.org/tagging

HTML input for many different options

I am making an HTML form with multiple inputs. I am using mainly select inputs (as below),
<select>
<option></option>
<option></option>
</select>
and also some radio buttons. I each input represents a different category. I have one category with 20+ possible options. I need an HTML form to do this without taking up a lot of space. Radio buttons wouldn't work, and a select input with 20+ possible options seems a bit over-the-top. Any ideas on what type of input I should use?
Or, is it possible to limit the length of the drop-down box, and have a scroll bar on the side?
As you asked:
Any ideas on what type of input I should use?
You could just use the size attribute of select:
<select size='5'>
<option>example</option>
<option>example</option>
<option>example</option>
<option>example</option>
<option>example</option>
<option>example</option>
<option>example</option>
<option>example</option>
</select>
However as you can see with this there is no longer a dropdown - but the options are still there, with a controllable limit and a scrollbar.
Consider using a freeform text input box coupled to a <datalist> element that provides a list of suggested options once the user starts typing something in the box. You should provide a <select> fallback for browsers that do not yet support <datalist>, like so:
<input type="text" name="{NameOfYourField}" list="datalist-dogs" />
<datalist id="datalist-dogs">
<select name="{NameOfYourField}">
<option>Affenpinscher</option>
<option>Afghan Hound</option>
<option>Aidi</option>
...
</select>
</datalist>
Be aware that users will be able to submit unique values. Unlikes radios and select boxes, input values are not restricted to the options provided in the datalist.
Further reading: https://developer.mozilla.org/en/docs/Web/HTML/Element/datalist
You can use CSS styling on HTML forms to scale the size down, just make sure to give the specific form elements an ID. You can use width and height commands on most [not all] types of form content. If you need the form to be really small, but then expand the form based upon user interaction, you can use JavaScript "onclick" or "onmouseover" commands to cause the form elements to expand when the user either rolls the mouse over it or clicks on it. I'm gonna show you an example of what this looks like on a submit button. The same model works with any form element
HTML form could look like this:
<input id="button" name="button" type="submit" value="Send">
CSS looks like this:
#button {
height: 5px;
width: 20px;
};
How HTML looks like with required JavaScript commands:
<input id="button" name="button" type="submit" value="Send" onmouseover="this.style.height='50px', this.style.width='200px'>

Increase spacing between consecutive datalist items

I am using a datalist to display a drop down as
<datalist id="ddl1">
<option value="one">Apple</option>
<option vlaue="two">Mango</option>
<option value="three">Cherry</option>
</datalist>
How can I increase the spacing between 2 datalist rows. If I try adding
<option value="one">One<br /></option>
This shows up fine in Safari but shows a <br /> as text in chrome.
How can this be resolved.
Unfortunately this is not possible as of yet. Recent versions of Chrome don't show the <br /> as text anymore, but also don't render a newline in the dropdown.
Generally this should be fixed with CSS as it's a styling problem, but that doesn't work either. The actual datalist element only provides values for the suggestion dropdown, but the element itself is not shown. The list you see resides in the shadow DOM, just like (for example) the up/down arrows on an <input type="number" />.
One would hope for some datalist pseudo-selectors like input::-webkit-outer-spin-button that Chrome has for the number controls, but none have been specced or implemented for datalist at all.

Purpose of <option label="...">

What's the purpose of the label attribute in the <option> tag in HTML5?
All the spec has to say is:
Specifies a label for the option.
MDN provides an explanation I cannot understand:
This attribute is text for the label indicating the meaning of the
option. If the label attribute isn't defined, its value is that of the
element text content.
Usage note: the label attribute is designed to contain a short label
typically used in a hierarchical menu. The value attribute describes a
longer label designed to be used near a radio button, for example.
I wrote a simple case I thought that could shed some light:
<select name="country">
<option value="ES" label="Spain's label">Spain</option>
<option value="FR" label="France's label">France</option>
</select>
... and only found that browsers seem to:
Ignore it (Firefox 26)
Completely replace tag content with it (Explorer 11, Chrome 32, Opera 12)
What is the attribute meant for?
Note: original question assumed the attribute was new. That's incorrect. It's only been enhanced due to newer tags introduced in HTML5.
In practice, it is meant for use inside a datalist element. The idea is that when browsers that do not support this element encounter it, they render its content, by normal principles, and if you want that fallback content to be empty, you need to use elements with empty content there. The label attribute lets you specify a human-readable string for an option, and modern browsers still implement the datalist with option elements properly.
Consider the following example in HTML5 CR:
<label>
Sex:
<input name=sex list=sexes>
<datalist id=sexes>
<option value="Female">
<option value="Male">
</datalist>
</label>
It is implemented so that there is just the text box, but if you type “f” there, the modern browsers suggest “Female”. (There is differences in details here, but that’s not relevant to the question.) Here you don’t need the label attribute.
But if you wanted to have values like 2 and 1 (the ISO/IEC 5218 standard codes for sexes) in the submitted form data, instead of strings “Female” and “Male”, what would you do? Inside a select element you could use <option value=2>Female</option>, but inside a datalist, that would result in the string “Female” being displayed by old browsers, and that would look odd.
Using the label attribute, you can write the datalist element as follows:
<datalist id=sexes>
<option value="2" label="Female">
<option value="1" label="Male">
</datalist>
This is meant to use human-readable words in the user interface and to submit the coded value 2 or 1 as part of form data. In practice, it does not work quite that well. The browser also has to show the coded value, since that’s what will appear in the textbox when the user selects a suggestion made by a browser. Different browsers have solved this in different ways, all with some drawbacks. E.g., on IE, focusing on the text box opens a menu with the alternatives “Female” and “Male”, which is fine, but then, if you click on “Female”, the menu closes and the character “2” appears in the box. It is difficult to say how browsers should deal with this. Anyway, this is where the label attribute is meant to be used.
Looking at this: http://blog.paciellogroup.com/2012/08/notes-on-html5-accessibility-support-in-ie-10/
It's looks like it's more used when you define a separate <datalist> for use as a list for an input.
My other thoughts are around usage for screen readers, however, I can't find any evidence of that.
Remember that <option> isn't limited to use in a <select> element, therefore some properties are more useful when included as part of <optgroup> et al.
Hope this helps.

select vs multiple with required attribute behaves inconsistently

I have a basic form like so:
<form id="test-form">
<select id="multi" name="mymulti" multiple required>
<option value="">Choose a different Option</option>
<option>Foo</option>
<option>Bar</option>
<option>Baz</option>
</select><br>
<select id="single" name="myselect" required>
<option value="">Choose a different Option</option>
<option>Foo</option>
<option>Bar</option>
<option>Baz</option>
</select> <br>
<input type="submit">
</form>
The key point here is two selects, both required, but one is multiple and the other is not.
If you choose the default options (note you actually have to click on them), and then submit the form, it will tell you that the single select is required, but not the multiple select.
I looked at the relevant portion of the html5 spec, and although there is some talk about how the required select interacts with placeholder elements, I don't see anything about how multiple+required behaves.
The above behaviour is only in Chrome, other browsers seem to behave the same for both types. My question is, why? It seems... inconsistent. I understand how to work around it with some javascript, just not why I would have to? Can anyone explain the rationale behind this?
Demo (remember to actually choose the placeholder options).
Chrome is acting right here. While implementation in FF is simply simpel. But chrome's implementation does not only follow the spec, it is also simply logic. If a mutliple or size > 1 select is used, there is no placeholder by definition. And without a selected attribute there is indeed nothing :checked (i.e.: selected) initially.
In case of a single select with size=1. The select > option:first-child with an empty value is the placeholder option. And a single select has always one option :checked/selected.
Here is a definition of placeholder option in JS: https://github.com/aFarkas/webshim/blob/gh-pages/src/shims/form-shim-extend.js#L88-94 and here a definition of valueMissing for select: https://github.com/aFarkas/webshim/blob/gh-pages/src/shims/form-shim-extend.js#L128-130