Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8918973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:53:19+00:00 2026-06-15T05:53:19+00:00

I have the following models: var allCategories = [{ id: 1, name: ‘Red’}, {

  • 0

I have the following models:

var allCategories = [{
    id: 1,
    name: 'Red'},
{
    id: 5,
    name: 'Blue'}];

function model() {
    self = this;
    self.name = ko.observable("");
    self.categoryId = ko.observable(-1);
    self.categoryName = ko.computed(function() {
        if (self.categoryId() == -1) return "";
        return getCategoryNameById(self.categoryId()).name;
    });
}

function getCategoryNameById(id) {
    return _.find(allCategories, function(cat) {
        return cat.id == id;
    });
}

I want to offer a dropdown to select the category but I have no clue how to bind that.
Maybe I’ve used the wrong approach with my models but that’s most likely how I get my data from the server so I’ve tried to wrap my JS around that.

I tried something like this:

<select data-bind="options: categories, optionsText: 'name', value: 'id', optionsCaption: 'Categorie...'"></select>

But I don’t get how to connect the dropdown value to the model categoryId.

Here is a fiddle with a working binding for the name property.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:53:22+00:00Added an answer on June 15, 2026 at 5:53 am

    For your list box you need to specify: options, optionsText, optionsValue, and value.
    value (which is the currently selected value) should point to your model.categoryId(). And optionsValue is the property name where to get values for the list:

    <select data-bind="options: categories, optionsText: 'name', optionsValue: 'id', value: $root.model.categoryId(), optionsCaption: 'Categorie...'"></select>
    

    And that’s it. And the working fiddle: http://jsfiddle.net/Y7Nrc/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following situation: var Task = Backbone.Model.extend({ initialize: function() { }, save:
I have following models setup in my Django application class School(models.Model): name = models.TextField()
I have the following models class Person(models.Model): name = models.CharField(max_length=100) class Employee(Person): job =
I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I have the following partial InfoWindowContent view. @model CasWeb.Models.ViewModels.AssetLocation <div> <h2>@Model.Name</h2> <table> <tr> <td>
I have the following code: var Company = function(app) { this.crypto = require('ezcrypto').Crypto; var
Lets say we have following models. class User(db.Model): username=db.StringProperty() avatar=db.ReferenceProperty() class User(db.Model): username=db.StringProperty() avatar=db.StringProperty()
I have the following models: class ProjectUser(models.Model): categories = models.ManyToManyField('UserCategory', blank=True, null=True) user_id =
Right now I have the following code: @model IEnumerable<MvcAuction.Models.Furniture> @{ ViewBag.Title = Search; }
I have the following code. Index.cshtml: @using System.Web.Script.Serialization @model MvcApplication3.Models.Person <script src=../../Scripts/knockout-2.1.0.js type=text/javascript></script> <!--

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.