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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:33:05+00:00 2026-06-10T03:33:05+00:00

I am having an issue with the knockoutjs if binding. I use the binding

  • 0

I am having an issue with the knockoutjs if binding. I use the binding quite liberally because it allows me to reference sub properties in my bindings without errors.

The one cause for concern is registering event handlers and so forth on DOM elements which are added by the if binding. Basically I need to get a callback when the elements added by the if binding are added to the DOM. I have tried using the livequery jquery plugin but it seems like the elements are being added twice. I came up with some example code to show this.

The knockout docs clearly state:

“The if binding, however, physically adds or removes the contained markup in your DOM, and only applies bindings to descendants if the expression is true.”

http://knockoutjs.com/documentation/if-binding.html

However in the following code, the alert in the inline script IS executed:

JS:

var vm = {
    val: ko.observable(false),
    someText: ko.observable("some text"),
};

ko.applyBindings(vm, document.getElementById("d1")

HTML:

<div id="d1" data-bind="if: val">
   <span data-bind="text: someText"></span>
   <script>alert("here")</script>
</div>​
  • 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-10T03:33:07+00:00Added an answer on June 10, 2026 at 3:33 am

    You have essentially answered your own question with this quote:

    “The if binding, however, physically adds or removes the contained
    markup in your DOM, and only applies bindings to descendants if the
    expression is true.”

    While this quote clearly indicates the behavior of bindings, it does not state anything about other JavaScript or browser functionality. This makes perfect sense since Knockout’s domain are its bindings and nothing else. By the time Knockout has been loaded, your browser will have already executed that alert-statement. And it will execute that alert-statement every time Knockout adds it back to the DOM-tree.

    Using <script> elements like this in combination with Knockout is bad practice and can lead to unpredictable behavior. What you want to do is express the intended behavior in the domain of Knockout, which are its bindings. In your case, this would fit the afterRender binding:

    <div id="d1" data-bind="if: val">
       <span data-bind="text: someText, afterRender: alert('here')"></span>
    </div>​
    

    If val is true then the inner <span> is rendered and the afterRender event will fire.

    If val is false then the inner <span> will be not be rendered and the afterRender event will not fire.

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

Sidebar

Related Questions

I am having issue with loading test.xml and test.properties inside the same folder conf.
Having issue with slider navigation. Works perfectly fine when there are no other unordered
I having issue that content assistant / intellisense is working in methods such as
Hi guys I am having issue I have this query: SELECT * FROM useraccount
i am having issue with loading my selector via a jquery ajax load call
Just started mongo and started having issue with querying already. i have a collection
Having an issue with a salesforce login on our site. When the customer fills
Having an issue resizing the #main_wrap on: http://www.coffeeproteindrink.com/method-athlete/ #main_wrap { min-height: 100%; background: url(images/content_back.png)
Having an issue with random individuals trying to access an intranet site with a
Having an issue here that I have tried everything I can think of but

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.