Create a custom autocomplete list for an input field - html

I need to create a custom auto-complete list of suggestions for an input field. So far I have associated an event handler to the input event to the html input element, so that when that event triggers, I can fetch the suggestions.
The problem is how would I show these suggestions. By default, input elements can display suggestions, but is it possible to customize/access those suggestions?
If not, what would be the alternatives?
Preferably I would like not to use external libraries.

You may use 'datalist' tag but it doesn't work in IE <= 9
<!DOCTYPE html>
<html>
<body>
<form>
<input list="country" name="countru">
<datalist id="country">
<option value="U.S.">
<option value="France">
<option value="China">
<option value="Cambodia">
<option value="Chile">
<option value="Canada">
<option value="Poland">
</datalist>
<input type="submit">
</form>
</body>
</html>
Run the code and try typing 'C' and then 'a'.

Related

Required function is not working in HTML form

I already apply <Required> tag in HTML <Select> but not working. The form is still able to proceed to the next step even not select anything at first step. Anyone know to solve it?
<label>Region :</label>
<select id="Region" class="custom-select form-control" required>
<option value="">Select Region</option>
<option value="central" id="Central">Central</option>
<option value="northern" id="Northern">Northern</option>
</select>
Have you declared your web page using HTML5? Make sure you declare the following statement at the beginning of the page
<!DOCTYPE html>
The required attribute is only available in HTML5 and only supported by specific browsers... w3schools Required Attribute
Having tested your original code in HTML5, it works.
You could get rid of the select option tag entirely, and place the value select option in the select tag itself.
<label>Region:</label>
<select value="Select Option" id="Region" class="custom-select form- control" required>
<option value="central" id="Central">Central</option>
<option value="northern" id="Northern">Northern</option>
</select>
Your code works it depends on the browser you use or the way that you are using it. I wrote a sample code for you. Hope it may help you:
<!DOCTYPE html>
<html>
<body>
<form >
<label>Region :</label>
<select id="Region" class="custom-select form-control" required>
<option value="">Select Region</option>
<option value="central" id="Central">Central</option>
<option value="northern" id="Northern">Northern</option>
</select>
<input type="submit">
</form>
</body>
</html>
So in the code above if you do not choose anything it will not let you submit.

HTML5 datalist see a value have other

I need a way to text something e recive otherthing, make me explain...
This code don't work because if I text Ba he don't tip me back Banana, but I need the code of Banana.
<form method="POST" action="connect.php">
<input list="nd" name="nd" />
<datalist id="nd">
<option value="1">Banana</option>
<option value="2">Apple</option>
<option value="3">Pasta</option>
<option value="4">Pizza</option>
</datalist>
<input type="submit" name="submit" />
I need to text "Ba" and have the autocomplete advice of "Banana", but once have found it I need 1 in the form.
Ty all and sorry for my terrible english :)
The values need to implement the word; not a number
<datalist id="nd">
<option value="Banana"></option>
<option value="Apple">Apple</option>
<option value="Pasta">Pasta</option>
<option value="Pizza">Pizza</option>
</datalist>
This will allow the autocomplete of the input text.
If you need to return a number; I would suggest either code behind or javascript to map the word value to a number and populate a hidden field or allow the php to handle the mapping (decoupling). Hope this helps.

imacros - dropdown select code not functioning

I have a code to select an option from a dropdown box which seems to work on one page of a website, but does not function on the other.
The code that works looks like this:
TAG POS=1 TYPE=SELECT FORM=NAME:quickOutputTemplate ATTR=NAME:saveToMenu CONTENT=$Save<SP>to<SP>Other<SP>File<SP>Formats
for this html:
<script type="text/template" id="quickOutputTemplate"> <input type="hidden" value="other" name="selectedQOFormat" /> <select class="saveToMenu" id="saveToMenu" name="saveToMenu" onchange="saveOutputForm('UA_output_input_form'); return false;"> <option value="enw">Save to EndNote online</option> <option value="endnote">Save to EndNote desktop</option> <option value="rid">Save to ResearcherID - I wrote these</option> <option selected="selected" value="other">Save to Other File Formats</option> </select> </script>
However when I try to use the same format:
TAG POS=1 TYPE=SELECT FORM=ID:cr_quickOutputTemplate ATTR=ID:cr_saveToMenu CONTENT=$Save<SP>to<SP>Text<SP>File
It does not work for this html:
<script type="text/template" id="cr_quickOutputTemplate"> <select class="saveToMenu" id="cr_saveToMenu"> <option selected="selected" value="other">Save to Text File</option> <option value="xls">Save to Excel File</option> </select> </script>
I have tried all the variations on this code that I can think of to make this work, but the macro just doesn't seem to be able to find the form. If any one has any ideas that might fix this, I would be very grateful to hear them.
Thanks
Try to use EVENT. Uncheck the ID recording option and record different types of drop downs. Then check which property in EVENT is changing and you can put there {{!LOOP}}.

How to make select elements required?

With input of type text the attribute required is available. It is not the case for select inputs. So how to make them required ?
FIDDLE
<form>
<select required>
<option></option><!--If this is selected require pop up will appear -->
<option>test</option><!--If this is selected form will be submitted -->
</select>
<input type="submit">
</form>
You can make them required by using html5 attribute required just like below.
<select required>
<option value="">select an option</option>
<option value="value1">Value 1</option>
<option value="value2">Value 2</option>
</select>
View Example in jsfiddle http://jsfiddle.net/88rXX/
Set a default value then on form submission check to see if that default value has changed.
If you're using the JQuery validation plug-in, try this Validate select box
You do have to remember though that just because it's validated client side, doesn't mean you shouldn't also check server side.
use it based on html 5. otherwise you can use any plugin

Blank HTML SELECT without blank item in dropdown list

How implement subj?
when i write:
<form>
<select>
<option value="0">aaaa</option>
<option value="1">bbbb</option>
</select>
</form>
then default selected item is "aaaa"
when i write:
<form>
<select>
<option value=""></option>
<option value="0">aaaa</option>
<option value="1">bbbb</option>
</select>
</form>
then default selected item is blank, but this blank item presents in drop down.
how i can implement SELECT tag with default blank value that hidden in dropdown list?
Just use disabled and/or hidden attributes:
<option selected disabled hidden style='display: none' value=''></option>
selected makes this option the default one.
disabled makes this option unclickable.
style='display: none' makes this option not displayed in older browsers. See: Can I Use documentation for hidden attribute.
hidden makes this option to don't be displayed in the drop-down list.
You can by setting selectedIndex to -1 using .prop: http://jsfiddle.net/R9auG/.
For older jQuery versions use .attr instead of .prop: http://jsfiddle.net/R9auG/71/.
Simply using
<option value="" selected disabled>Please select an option...</option>
will work anywhere without script and allow you to instruct the user at the same time.
<select>
<option value="" style="display:none;"></option>
<option value="0">aaaa</option>
<option value="1">bbbb</option>
</select>
Here is a simple way to do it using plain JavaScript. This is the vanilla equivalent of the jQuery script posted by pimvdb. You can test it here.
<script type='text/javascript'>
window.onload = function(){
document.getElementById('id_here').selectedIndex = -1;
}
</script>
.
<select id="id_here">
<option>aaaa</option>
<option>bbbb</option>
</select>
Make sure the "id_here" matches in the form and in the JavaScript.
You can't. They simply do not work that way. A drop down menu must have one of its options selected at all times.
You could (although I don't recommend it) watch for a change event and then use JS to delete the first option if it is blank.
For purely html #isherwood has a great solution. For jQuery, give your select drop down an ID then select it with jQuery:
<form>
<select id="myDropDown">
<option value="0">aaaa</option>
<option value="1">bbbb</option>
</select>
</form>
Then use this jQuery to clear the drop down on page load:
$(document).ready(function() {
$('#myDropDown').val('');
});
Or put it inside a function by itself:
$('#myDropDown').val('');
accomplishes what you're looking for and it is easy to put this in functions that may get called on your page if you need to blank out the drop down without reloading the page.
You can try this snippet
$("#your-id")[0].selectedIndex = -1
It worked for me.