So I am having a bit of an issue getting my HTML5 Datalist to populate dynamically from a javascript array that is being populated from values of a key of an object that is being populated via rows in a MySQL database. Phew!
MySQL Database => Table => Rows => JSON => Javascript Objects => "firstname" & "lastname" key => Array of first names => Datalist Options.
I successfully created the Array of names:
var nameArray = ["Rick Bross","Madison Smith","Jack Johnson"]; //Example of my array
And set up a loop to .append them to the datalist:
for (var i = 0; i < nameArray.length; i++) {
alert(i + " - " + nameArray[i]); //Works Fine, "0 - Rick Bross", "1 - Madison Smith", etc.
$('#potentials').append("<option value='" + nameArray[i] + ">"); // Not working.
}
Here is my HTML:
<input tabindex='1' list="potentials" type="text" placeholder="First & Last Name" id="name" name="name"></input>
<datalist id="potentials">
</datalist>
Why this isn't populating?
The datalist tag is introduced in HTML5. The <datalist> tag should be used with an <input< element that contains a "list" attribute. The value of "list" attribute is linked with the datalist id.
Unfortunately, Internet Explorer and Chrome are the only browsers to support datalists on range inputs at this time.
Select input element presents options for the users from which they need to select one of them. On the otherhand, Datalist presents a list of suggested values to the associated input form (text) field and users are free to select one of those suggested values or type in their own value.
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.
There was a missing apostrophe, try:
$('#potentials').append("<option value='" + nameArray[i] + "'>");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With