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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:51:46+00:00 2026-06-07T20:51:46+00:00

I constantly find myself using this idiom in KO-based HTML templates: <!– ko if:

  • 0

I constantly find myself using this idiom in KO-based HTML templates:

<!-- ko if: isEdit -->
<td><input type="text" name="email" data-bind="value: email" /></td>
<!-- /ko -->
<!-- ko ifnot: isEdit -->
<td data-bind="text: email"></td>
<!-- /ko -->

Is there a better/cleaner way to do conditionals in KO, or is there a better approach than just using traditional if-else constructs?

Also, I would just like to point out that some versions of Internet Explorer (IE 8/9) don’t parse the above example correctly. Please see this SO question for more information. The quick summary is, don’t use comments (virtual bindings) inside table tags to support IE. Use the tbody instead:

<tbody data-bind="if: display"><tr><td>hello</td></tr></tbody>
  • 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-07T20:51:48+00:00Added an answer on June 7, 2026 at 8:51 pm

    There are a couple different ways that you can handle this type of code.

    • with an if/ifnot combination like you are now. This works fine and is not terribly verbose.

    • Michael Best’s switch/case binding (https://github.com/mbest/knockout-switch-case) is quite flexible and can let you easily handle this and more complicated ones (more states than true/false).

    • Another option is to use dynamic templates. You would bind an area to one or more templates with the template name being used based on an observable. Here is a post that I wrote on this topic a while back: http://www.knockmeout.net/2011/03/quick-tip-dynamically-changing.html. In your scenario, it might look like:

    <td data-bind="template: $root.getCellTemplate"></td>
    
    <script id="cellEditTmpl" type="text/html">
        <input type="text" name="email" data-bind="value: email" />
    </script>
    
    <script id="cellTmpl" type="text/html">
        <span data-bind="text: email"></span>
    </script>
    

    The getCellTemplate function could live wherever, but would be given the item ($data) as the first argument and would return the name of the template to use.

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

Sidebar

Related Questions

I constantly find myself writing similar code like the example below: if (object[Object Name]
I come from a c++ background and I find myself constantly doing this in
I find myself doing something like this constantly to pull GET args into vars:
I constantly find myself sending .NET code snippets to other developers using MS outlook
I've been using Java for a year or so now, and I constantly find
I find myself constantly learning new things in web development and there is always
Even as a somewhat seasoned JS dev I find myself constantly suprised at shallow
While debugging I constantly find myself adding System.Web.HttpContext.Current.Request or similar to QuickWatch Expression (Shift+F9)...
I constantly find myself in a rattle between languages, technology and solutions when trying
Constructing scala.collection.Map from other collections, I constantly find myself writing: val map = Map(foo.map(x=>(x,

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.