Skip to content Skip to sidebar Skip to footer

Editable Combo Box Javascript And HTML

I need to do the following thing: Using JavaScript, the HTML input tag having the text type and a select tag (combo box), create an editable combo box. I have no idea how to make a

Solution 1:

HTML5 includes the datalist element which solves this problem! :)

<html>
    <label>Your preferred programming language: </label>
    <input type="text" list="combo-options" id="combobox">
    <datalist id="combo-options">
        <option value="ActionScript">ActionScript</option>
        <option value="AppleScript">AppleScript</option>
    </datalist>
</html>

Solution 2:

Try something like this:

<label>Your preferred programming language: </label>
<input type="text" id="new_value"/>
<select id="combobox">
    <option value="">Select one...</option>
    <option value="ActionScript">ActionScript</option>
    <option value="AppleScript">AppleScript</option>
</select>



$("#new_value").keyup(function (e) {
    if (e.keyCode == 13) { // enter key
        var value = $(e.currentTarget).val();   // store the value from the input tag
        var option = $("<option/>");            // create a new option tag
        option.val(value).text(value);          // set the value attribute as well as the content
        $("#combobox").append(option);          // insert the new object to the dom
    }
});

Post a Comment for "Editable Combo Box Javascript And HTML"