Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use datalist with HTML textarea?

I wanted to check if it's possible to use datalist with a textarea. I have it working with an input field, but the task I have here requires me to "add input suggestions to a one row textbox with datalist". Does that even make any sense?

            <fieldset>
                <legend>
                    Välj favoritbild med hjälp av datalist - skriv i listan nedan
                </legend>
                <input list="bilar" name="bil">
                <!--<TEXTAREA list="bilar" NAME="bil" ROWS="1"></TEXTAREA>  -->
                <datalist id="bilar">
                    <option value="Dodge Viper">
                    <option value="Honda Civic">
                    <option value="Corvette Z06">
                    <option value="Volvo V70">
                    <option value="Ferrari Spider 360">
                </datalist>                 
            </fieldset>

However, I can't get it working with my textarea (which is commented out above). It works with the input-element however.

Anyone know how I can get the above example working with a one-row textbox?

like image 711
user1531921 Avatar asked May 09 '14 07:05

user1531921


People also ask

Can we use value attribute in textarea?

<textarea> does not support the value attribute.

How the Datalist tag can be used in HTML?

The <datalist> tag is used to provide an "autocomplete" feature for <input> elements. Users will see a drop-down list of pre-defined options as they input data.

What is alternative for Datalist?

Alternatives to <datalist> If you can actually limit the user's input to the predefined options, and you don't need free-entry, you can simply use the standard <select> element. If you need type-entry autocomplete, the most widely supported solution is Javascript.

Is Datalist supported by all browsers?

You can see that each browser displays a tick mark for each datalist option provided. Additionally, Chrome will snap the slider to these predefined values as the user moves the slider near them. Unfortunately, Internet Explorer and Chrome are the only browsers to support datalists on range inputs at this time.


1 Answers

No, because datalist by definition associates with an input element. Moreover, selecting an item drom a datalist means replacing the entire value of the associated input element, instead of appending to it, which normally be the idea if you used predefined alternatives for a textarea element.

There is nothing illogical with the idea of a pre-made list of alternatives to use for text area input, but there is nothing for it in HTML at present (or being planned, as far as I known). It can be coded rather simply in JavaScript, though.

You can have a list of items, for example as a ul list, or even as a select element. You would then add a little JavaScript that causes the item text to be written or appended to the textarea element e.g. when an item is clicked on or a selection is made from a select list

There is normally no reason to have a single-line text input field as a textarea. It’s possible, just not useful, except perhaps in very special cases. Note that <textarea rows=1 ...> lets the user enter any number of lines, it just makes that very inconvenient.

like image 173
Jukka K. Korpela Avatar answered Oct 07 '22 23:10

Jukka K. Korpela