forum

Home / DeveloperSection / Forums / How to add item in list box using knockoutjs

How to add item in list box using knockoutjs

Anonymous User198618-Feb-2015
I am trying to add item in list box

This is my view code:


<divclass="container"style="max-width: 400px; border: 1pxsolid#e2e2e2; padding: 50px; margin-top: 30px">
        <br/>
        <divclass="row">
            <divclass="row">
                <divclass="col-md-4">
                    Item Name:
                </div>
                <divclass="col-md-6">
                    <inputtype="text"class="form-control"data-bind='value: itemToAdd, valueUpdate: "afterkeydown"'/>
                </div>
                <divclass="col-md-2">
                    <buttontype="submit"class="btn btn-primary"data-bind="enable: itemToAdd().length > 0 ">Add</button>
                </div>
            </div>
            <br/>
            <divclass="row">
                <divclass="col-md-4">
                    Item List:
                </div>
                <divclass="col-md-8">
                    <selectmultiple="multiple"class="form-control"style="height: 100px"data-bind="options: items"></select>
                </div>
            </div>
        </div>
    </div>

 

And this is my script code:

$(function () {
   var SimpleListModel = function (items) {
   this.items = ko.observableArray(items);
   this.itemToAdd = ko.observable("");
   this.addItem = function () {
   if (this.itemToAdd() != "") {
       this.items.push(this.itemToAdd());
       this.itemToAdd("");
    }
 }.bind(this);
     };
     ko.applyBindings(new SimpleListModel());
 });

This is my complete code but item is not add in list box. I don’t know where is create problem of my code. Please solve my problem as soon as possible.

Thanks in advance!!



Updated on 18-Feb-2015
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By