Jump to content

datalist object textbox


Misulache

Recommended Posts

I don't know how to insert the text written in the datalist textbox as a new item in the datalist options. In fact I don't know how to read the text written in the datalist textbox in a variable. I've tried to read it as I would do with a text input but it doesn't work.

Link to comment
Share on other sites

I've already read the example your link get me to. Have you really tried to get the text from the textbox of the datalist? This is what I want and I didn't found any example.

Link to comment
Share on other sites

Its done very much like you would do normally, use id ref getElementById(), or use getElementsByName()[0] and retrieve the value, with the text, it is similar to creating new option for select dropdown.

            function myFunction(e) {                e.preventDefault();                var x = document.getElementById("browserListInput"); //option 1                //var x = document.getElementsByName("browser")[0]; // option2                alert(x.value);                x.value = "";                var y = document.getElementById("browsers");                var option = document.createElement("option");                option.value = "Another Browser which is bound to be better than IE";                y.appendChild(option);            }        <form>            <input  id="browserListInput" list="browsers" name="browser">            <datalist id="browsers">                <option value="Internet Explorer">                <option value="Firefox">                <option value="Chrome">                <option value="Opera">                <option value="Safari">            </datalist>            <button onclick="myFunction(event)">Click Me</button>        </form>        <p id="demo"></p>
Link to comment
Share on other sites

Thanks, it seems that the correct instructions sequence is:

x=getElementById("browserListInput");

option.value=x.value;

If you would given:

option.value=getElementById("browserListInput").value;

the alert box would display "undefined". How did you know it?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...