Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove optionsCaption from select tag using knockout JS

How to remove the optionsCaption from select tag ? using knockout JS

My select tag is like :

<select data-bind="options: categories, optionsText: 'type', optionsCaption: 'Select Any Ticket type', value: chosenCategory, disable: showReservationDetails, event: {change: calRemainingTickets}"></select>

It shows Select Any Ticket as first option. on change of the select tag i want to remove the Select Ant Ticket option.

How can we remove that required option from select tag ?

Thank you in Advance .

like image 529
Nimmagadda Gowtham Avatar asked Nov 20 '25 05:11

Nimmagadda Gowtham


1 Answers

You can bind optionsCaption to an observable and set the observable's value to undefined. I've modified Joe's code to do this.

var vm = function () {
    this.optionsCaption = ko.observable('Select any');
    this.categories = ko.observableArray([ {type: 'Type 1' }, { type: 'Type 2' }]);
    this.chosenCategory = ko.observable('Select Any Ticket type');
    this.showReservationDetails = ko.observable(false);
    this.calRemainingTickets = function () {
        this.optionsCaption(undefined);
    }.bind(this);
}

ko.applyBindings(new vm());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.0.0/knockout-min.js"></script>
<select data-bind="options: categories, optionsCaption: optionsCaption, optionsText: 'type', value: chosenCategory, disable: showReservationDetails, event: {change: calRemainingTickets}"></select>
like image 74
Roy J Avatar answered Nov 21 '25 20:11

Roy J



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!