I have a form for editing existing data. The user chooses a record and then the form is pre-populated by a database query. Some of the form fields are presented as datalists (allowing pre-populated choices, but users can enter their own).
What I would like to do is to have these datalist elements appear with the current value for that key (from the db) showing in the text box atop the datalist - i.e., in the same manner that an <option> value can be matched to options in a <select> input and show that option in the <select> box when it is rendered.
I've added selected to the options list like this:
<input type="search"
name="Status"
placeholder = "~ Status ~"
id="Status"
list="statuslist">
<datalist id="statuslist">
<select>
<option value="launched">launched</option>
<option value="pre-launch" selected>pre-launch</option>
<option value="no company">no company</option>
</select>
</datalist>
...but that doesn't seem to do the trick. The datalist input box always displays ~ Status ~ (the placeholder). If I remove the placeholder, it's just blank.
Does datalist support this kind of functionality? How do I make this happen?
datalist.js doesnt update with the selected value, you have to add this after the datalist initialization
//traverse each list
$('input[list]').each(function(){
// variable for the jquery object of the selected option
var datalist = $('#' + $(this).attr('list') + ' option:selected');
// check if value, but also check if option has the attribute "selected"
if(datalist.val() != 'undefined' && datalist.attr('selected')){
// update selected to the pre-selected
$(this).attr('value',datalist.text());
}
});
updated jsfiddle
Edit: And added support for multiple lists as requested, and fixed error with the datalist.js fallback solution to avoid overwriting the placeholder if nothing selected.
Related
Even after passing label attribute its showing both label and value in dropdown also, after selecting option from dropdown its showing value in input box insteed of label
<input
on:change={onChangeSuggestion}
on:input={updateValue}
list="inventoryLocations"
id="store-locator-mapbox-input"
placeholder="Search store or address..."
type="text"
/>
<datalist id="inventoryLocations">
{#each suggestions as location}
<option value={[location.geometry.coordinates[0], location.geometry.coordinates[1]]} label={location.place_name} />
{/each}
</datalist>
How datalist options are presented depends on the browser, e.g. Firefox will not show the value in the list. The only constant is that the value will be what will end up in the input because inputs of type text have no hidden value (unlike select).
If you need more control over the appearance, the only real option is using a custom component that renders its own input/dropdown without relying on datalist. There are libraries which provide this functionality with varying features and accessibility.
Suppose I have an HTML Form which contains a drop down (and a submit button), through which a user can select any one of the unit of length and submit the form. The code for the drop down menu is show below :
<select name = "from_length">
<option value = "choose_1">Metre</option>
<option value = "choose_2">Kilometre</option>
<option value = "choose_3">Millimetre</option>
<option value = "choose_4">Yard</option>
<option value = "choose_5">Light Year</option>
</select>
I want to select a default option which shall be displayed to the user, when the user opens the webpage.
However, there's a twist. I don't want to use the selected attribute in order to show a default option to the user. This is because the default option being displayed to the user may change depending on certain conditions (for e.g. when user selects "Kilometre" and submits this form, the user must see "Kilometre" selected by default, and not "Metre" (the first option)).
I wish to know if it is possible to select the default value based on the value of the option being selected by the user using only HTML (i.e. no JavaScript)
Thanks in advance for helping me out !
The only way I see that you can do this is with PHP, which allows you to get access to form data. The file extension would also have to be changed from ‘html’ to ‘php’ for this to work.
<select name = "from_length">
<option value="choose_1"<?php if($_POST["from_length"]=="choose_1"){echo(" selected");} ?>>Metre</option>
<option value="choose_2"<?php if($_POST["from_length"]=="choose_2"){echo(" selected");} ?>>Kilometre</option>
<option value="choose_3"<?php if($_POST["from_length"]=="choose_3"){echo(" selected");} ?>>Millimetre</option>
<option value="choose_4"<?php if($_POST["from_length"]=="choose_4"){echo(" selected");} ?>>Yard</option>
<option value="choose_5"<?php if($_POST["from_length"]=="choose_5"){echo(" selected");} ?>>Light Year</option>
</select>
My code still uses the selected attribute, but on each line, the php code checks if this option has been selected by the user, and only if it has, then the 'selected' attribute is printed into the document.
I want to make input option in select tag so user can choose between options or insert different value.
Is it possible?
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
**<insert user value>**
</select>
HTML solution with "list" attribute:
<input type="text" name="city" list="citynames">
<datalist id="citynames">
<option value="Boston">
<option value="Cambridge">
</datalist>
You will have to use javascript to get the additional value. Check this post for some example code:
Jquery dynamically update "other" option in select
Select elements can't contain anything other than option or optgroup elements. Here's a ref to the spec http://www.w3.org/TR/html5/forms.html#the-select-element
You may be better off adding an option for "other" in your dropdown and then using JS to detect for that choice to dynamically show an input (below the dropdown) for a custom value.
Position an input box over the select box and remove the border of the input box. Make the length of the input box shorter than the select box so that the select box may still be used at its end.
Use the oninput event to detect input being entered in the input box. On each keystroke check for a continuing match in the select box. When a match no longer exists there is no further need for the select box.
The server will expect to receive both the text box input, if any, and the select box input, if any, and should use the select value if provided otherwise the input value if provided.
In my JSP I have a dropdownlist and a submit button when clicking the submit button I lose the value already selected in my list.
I am using the jstl because i need to construct other table corresponding the value selected in my list.For that I must call a submit button but the problem; it reset the value selected
I want to know if there is a way to save the value selected in my list even I click a submit button.
I work with JSP and eclipse environment.
Thank you for your help.
You need to preset the inputs with the request parameter values. You can access parameter values in EL by ${param.name}. In case of dropdowns rendered by HTML <select> element, you need to set the selected attribute of the HTML <option> element in question. You can make use of the ternary operator in EL to print the selected attribute whenever the option value matches the request parameter value.
Basic example:
<select name="foo">
<c:forEach items="${options}" var="option">
<option ${param.foo == option ? 'selected' : ''}>${option}</option>
</c:forEach>
</select>
I am using jstl with dropdown lists.
When i click submit button i success the specification but values int dropdownlists are reinitialized.
So I want to submit form without loosing the values already selected in the form because I need to stay always at the same level in the form.To be more clear, user choose a value from ddl and click edit button to show other options and fill them at the same form without loosing what he has selected.
I have tried to deal like that...
<form action="myjsp.jsp" method="post">
<input type="Submit" value="Edit">
...but it doesn't work.
Thank you for your help.
You need to preset the inputs with the request parameter values. You can access parameter values in EL by ${param.name}. Basically:
<input type="text" name="foo" value="${param.foo}">
Note that this is XSS sensitive. You always need to sanitize the user inputs. You can use the JSTL functions taglib for this.
<input type="text" name="foo" value="${fn:escapeXml(param.foo)}">
In case of dropdowns rendered by HTML <select> element, it's a bit trickier. You need to set the selected attribute of the HTML <option> element in question. You can make use of the ternary operator in EL to print the selected attribute whenever the option value matches the request parameter value.
Basic example:
<select name="foo">
<c:forEach items="${options}" var="option">
<option ${param.foo == option ? 'selected' : ''}>${option}</option>
</c:forEach>
</select>