KnockoutJS - with Binding


This binding is used to bind the child elements of an object in the specified object's context. This binding can also be nested with other type of bindings such as if and foreach.


with: <binding-object>


  • Pass the object which you want to use as context for binding child elements as the parameter. Child elements will not be shown if the object or expression passed is evaluated to be null or undefined.

  • The expression will be re-evaluated, if the parameter provided is an observable one. Correspondingly, the child elements will be re-processed based on the refreshed context result.


Let us take a look at the following example which demonstrates the use of with binding.

<!DOCTYPE html>
      <title>KnockoutJS with binding</title>
      <script src = ""
         type = "text/javascript"></script>
      <h1 data-bind = "text: siteName"> </h1>
      <table border = "1">
            <th>Course Name</th><th>Fees</th><th> Start Date</th>
         <tbody data-bind = "with: courses ">
               <td><span data-bind = "text: courseName"></span></td>
               <td><span data-bind = "text: Fees"></span></td>
               <td><span data-bind = "text: startDate"> </span></td>

      <script type="text/javascript">
         function AppViewModel() {
            self = this;
            self.siteName = ko.observable( 'TutorialsPoint');
   = ko.observable ({
               courseName:  'Microsoft .Net', 
               Fees: 20000, startDate: '20-Mar-2016'
         var vm = new AppViewModel();


Let's carry out the following steps to see how the above code works −

  • Save the above code in with-bind.htm file.

  • Open this HTML file in a browser.


Container-less with

There might be a situation when it is not possible to place data-binding inside a DOM element. Essential binding can still be performed with the help of container-less syntax based on the comment tags as shown in the following code.

   <li>Course Info</li>
   <!-- ko with: currentClasses -->
   <!-- /ko -->
   <!-- ko with: plannedClasses -->
   <!-- /ko -->

The <!--ko--> and <!--/ko--> works as start and end markers making it a virtual syntax and binds the data as if it is a real container.