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

  • SEARCH
  • Home
  • 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 8137399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:06:19+00:00 2026-06-06T11:06:19+00:00

I am writing a custom binding handler in Knockout. I want to to pass

  • 0

I am writing a custom binding handler in Knockout.
I want to to pass a String like: firstName as the valueAccessor, then I want to get the observable firstName from my view model, but how do I get an observable by String?

Here is my code:

ko.bindingHandlers.DefaultOrCustom = {
    update: function (element, valueAccessor, allBindingsAccessor, viewModel) {

        var value = valueAccessor(), allBindings = allBindingsAccessor();
        // value will equal "firstName" at this phase
    } 
}
  • 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-06T11:06:22+00:00Added an answer on June 6, 2026 at 11:06 am

    You can access a JavaScript object as an associative array, using your field name as the key (e.g. object.foo can be represented as object['foo']) . Then, as you are pointing to an observable, you call it as a function to get / set the value.

    Assuming you have a binding like this:

    data-bind="DefaultOrCustom: 'firstName'"
    

    You can access the observable like this:

    ko.bindingHandlers.DefaultOrCustom = {
    update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
    
        var value = valueAccessor(), allBindings = allBindingsAccessor();
    
        var oldValue = viewModel[value](); // read from the observable
        viewModel[value]('New Value');     // write to the observable 
    }
    

    You can get even more flexible by checking the type of the field. If it is ‘function’ then treat it is an observable – using the () form, otherwise treat it as a regular field.

    ko.bindingHandlers.DefaultOrCustom = {
    update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
    
        var value = valueAccessor(), allBindings = allBindingsAccessor();
        var oldValue;
    
        if (typeof(viewModel[value]) == 'function') {
          oldValue = viewModel[value]();   // read from the observable
          viewModel[value]('New Value');   // write to the observable
        } else {
          oldValue = viewModel[value];     // read from the field
          viewModel[value] = 'New Value';  // write to the field
        }
    }
    

    FYI: typeof is actually not a function, but I often find using this form makes it clearer what I am typing.

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

Sidebar

Related Questions

I am writing a custom WCF Binding and I want to run some code
I am writing a custom binding to use $.expandingTextarea with Knockout.js . Following the
I'm writing a custom makefile project using QtCreator and I want to delete a
I am constantly writing custom WebParts but after I deploy them, they always get
I am writing a custom control, and I have a property path as string
I'm aware of writing custom attributes that decorate a method and get evaluated before
Recently I've decided to get some knowledge about writing custom tags. And there is
I am writing a custom ConfigurationElementCollection for a custom ConfigurationHandler in C#.NET 3.5 and
I am writing a custom .pac script for use with Firefox. Following numerous examples
I'm writing a custom deserializer that will deserialize a list by deserializing each of

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.