Misulache Posted November 28, 2014 Share Posted November 28, 2014 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 More sharing options...
dsonesuk Posted November 28, 2014 Share Posted November 28, 2014 http://www.w3schools.com/jsref/coll_datalist_options.asp Link to comment Share on other sites More sharing options...
Misulache Posted November 28, 2014 Author Share Posted November 28, 2014 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 More sharing options...
dsonesuk Posted November 29, 2014 Share Posted November 29, 2014 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 More sharing options...
Deepak Namdev Posted November 29, 2014 Share Posted November 29, 2014 http://www.w3schools.com/jsref/coll_datalist_options.asp Hi sir, i want taken a validation on name.... <input type ="text" name="fname" id="fname"> pls help me Link to comment Share on other sites More sharing options...
dsonesuk Posted November 29, 2014 Share Posted November 29, 2014 Yea, and i want validation taken on those who attempt to post unrelated topic into someone else's topic, instead of their own. Link to comment Share on other sites More sharing options...
Misulache Posted December 2, 2014 Author Share Posted December 2, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now