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 8773707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:22:17+00:00 2026-06-13T18:22:17+00:00

I am wondering if it is possible with knockoutjs to pass arguments when binding.

  • 0

I am wondering if it is possible with knockoutjs to pass arguments when binding.

I am binding a list of checkboxes and would like to bind to a single computed observable in my viewmodel. In my viewmodel (based on parameter passed to the read function) I want to return true/false based on certain conditions.

var myViewModel=function(){
    this.myprop=ko.computed({read: function(){
    //would like to receive an argument here to do my logic and return based on argument.
}
});
};

<input type="checkbox" data-bind="checked: myprop(someval1)" />
<input type="checkbox" data-bind="checked: myprop(someval2)" />
<input type="checkbox" data-bind="checked: myprop(someval3)" />

Any suggestions?

  • 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-13T18:22:18+00:00Added an answer on June 13, 2026 at 6:22 pm

    Create a function whose sole purpose is to return a computed observable. It may take in parameters as you wanted. It will have to be a separate computed observable if you want it to be a two-way binding.

    Then in your binding, call that function with the appropriate arguments. The computed observable it returns will be bound to in your view and will update as usual.

    Here’s a fiddle where I used this technique for creating event handlers. You can do something similar here.

    You can keep it clean by making the function a method on the observable. Either by adding to the ko.observable.fn prototype or adding it directly to the observable instance.

    ko.observable.fn.bit = function (bit) {
        return ko.computed({
            read: function () {
                return !!(this() & bit);
            },
            write: function (checked) {
                if (checked)
                    this(this() | bit);
                else
                    this(this() & ~bit);
            }
        }, this);
    };
    // or
    function ViewModel() {
        this.flags = ko.observable(0);
    
        this.flags.bit = function (bit) {
            return ko.computed({
                read: function () {
                    return !!(this() & bit);
                },
                write: function (checked) {
                    if (checked)
                        this(this() | bit);
                    else
                        this(this() & ~bit);
                }
            }, this);
        }.bind(this.flags);
    }    
    

    Then apply to your view

    <input type="checkbox" data-bind="checked: flags.bit(0x1)"/>
    <input type="checkbox" data-bind="checked: flags.bit(0x2)"/>
    <input type="checkbox" data-bind="checked: flags.bit(0x4)"/>
    <input type="checkbox" data-bind="checked: flags.bit(0x8)"/>
    

    Demo


    However if you are just trying to bind all those checkboxes to a single value in your view model, you don’t need to do that. Use the checked binding on an array in your view model and give your checkboxes a value. Every checked value will be added to the array. And it will be a two way binding.

    <input type="checkbox" data-bind="checked: checkedValues, value: 1"/>
    <input type="checkbox" data-bind="checked: checkedValues, value: 2"/>
    <input type="checkbox" data-bind="checked: checkedValues, value: 3"/>
    <input type="checkbox" data-bind="checked: checkedValues, value: 4"/>
    
    var viewModel = {
        checkedValues: ko.observableArray([])
    };
    

    Demo

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

Sidebar

Related Questions

I'm wondering if its possible to make a list like this: Or should I
I'm wondering if it's possible to bind the selected value in a Twitter bootstrap
I am wondering is it possible to have a list box that has custom
I was wondering is it possible to add an +1 button (Facebook 'like' equivalent)
I was wondering is it possible to execute formulas with vlookup? Like: frequency date
I was wondering how (if possible) would you access the return value of an
I was wondering if its possible to get Android application info like (name, version,
I am wondering is it possible to detect when binding is done? For instance
I am wondering if is possible to create an XSLT stylesheet that would extract
I was wondering how (if possible) I can go about making an adjacency list

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.