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

The Archive Base Latest Questions

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

<div data-bind=foreach: passengers> <a href=# data-bind=’style: { color: isOwner ? orange : darkgreen },

  • 0
<div data-bind="foreach: passengers">                       
<a href="#" data-bind='style: { color: isOwner  ? "orange" : "darkgreen" }, 
text: person_username, click: function() { $root.removePassenger($data, $parent); } '>

Say I have a template like this. The click function should only be bound if isOwner is true. Is there a simple/easy way to do this? I’m guessing I could break out the full jquery templating and get something done, but I’d like to know a more elegant solution.

Thanks.

  • 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-15T04:17:20+00:00Added an answer on June 15, 2026 at 4:17 am

    What you’re asking for — handle a click event only when some property is set — is application logic. Application logic does not belong in the view. It belongs in the view model.

    Sure, you could do something like:

    click: function() { if ($data.isOwner) $root.removePassenger($data, $parent); }
    

    But, again, that’s putting logic in your view, which is frowned upon from a separation-of-concerns and debugging point of view, and it uglifies your HTML.

    I’d suggest doing it like this:

    <div data-bind="foreach: passengers">                       
       <a href="#" data-bind='style: { color: isOwner  ? "orange" : "darkgreen" }, 
          text: person_username, click: function() { $root.removePassengerIfApplicable($data, $parent);"</a>
    </div>
    

    And your JavaScript:

    function removePassengerIfApplicable(passenger, parent) {
        if (passenger.isOwner) {
            removePassenger(passenger, parent);
        }
    }
    

    UPDATE

    It wasn’t clear from your post that you didn’t want to show a link if isOwner = false. Here’s some updated code for you:

    <div data-bind="foreach: passengers">  
       <div data-bind="if: isOwner">                     
          <a href="#" style="color: orange" 
          text: person_username, click: function() { $root.removePassenger($data, $parent); }"</a>
       </div>
       <div data-bind="if !isOwner()">
          <span style="color: darkgreen" data-bind="text: person_username"></span>
       </div>
    </div>
    

    The above code shows a link if isOwner, and a plain text span if not.

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

Sidebar

Related Questions

Here's the relevant code: <div data-bind=foreach: chats, visible: chats().length > 0> <input data-bind='value: $parent.newCommentText'
I'm using knockout in the following manner successfully: <div id=Options data-bind=foreach: Options> <button type=button
I have this code: <ul id=chats data-bind=foreach: chats> <li> <div class=chat_response data-bind=visible: CommentList().length ==
<a href=#login_form class=the_form>click to Contact</a> The form: <div style=display:none> <form id=login_form method=post action=> <p
I have a bound list using knockoutjs / mvc3 <div data-bind=foreach: phones> <input data-bind='value:
I have set a background on the data-role=page element like so <div data-role=page style=background:
Say I have a select box eg <div data-bind='visible: someProp'> <select class=selectSubWidgets data-bind='options: subWidgets,optionsText:
<div style=display:none> <form id=login_form method=post action=> <p id=login_error>Please, enter data</p> <p> <label for=login_name>Login: </label>
How can I bind data from a function inside a KnockoutJS foreach ? e.g.
I have the following HTML segment: <table class=answerGrid data-bind=foreach: rows> <tr data-bind=foreach: $data> <td>

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.