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

  • Home
  • SEARCH
  • 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 6659761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:04:13+00:00 2026-05-26T02:04:13+00:00

My KnockoutJS template looks as follows: <div id=wrapper data-bind=template: { name:’theTemplate’, foreach: cars(), beforeRemove:

  • 0

My KnockoutJS template looks as follows:

       <div id="wrapper" data-bind="template: {
                                                name:'theTemplate', 
                                                foreach: cars(),  

                                                beforeRemove: function(elem) { 
                                                    console.log(elem);                                     
                                                    console.log('try removing..');
                                                    try {
                                                        jQuery(elem).hide().remove();
                                                    } catch (e) {
                                                        console.log(e);
                                                    }
                                                },

                                                afterAdd: function(elem) { 
                                                    console.log('try adding..');
                                                    try {
                                                        jQuery(elem).hide().fadeIn();
                                                    } catch (e) {
                                                        console.log(e);
                                                    }
                                                }
                                            }"></div>

When I’m removing a object from the cars() observableArray I want to hide() and remove() the element which is being removed. Except when removing a element the beforeRemove function is called three times with three different elements as parameter.

The three elements are:

  1. Comment { data=" Block 1 ", length=9, nodeName="#comment", meer...}
    try removing..

  2. <TextNode textContent=" ">
    try removing..

  3. <div class="block">
    try removing..

The afterAdd behaves the same way.. Is this a bug, or is my understanding of the function incorrect?

Thanks for your time!

  • 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-05-26T02:04:14+00:00Added an answer on May 26, 2026 at 2:04 am

    I believe that’s the expected behavior if your template contains those elements.

    If you only want to hide/show particular elements — say, <div> elements — you’ll want to do a jQuery selector to filter the elements down:

    afterAdd: function(elem) { 
        console.log('try adding..');
        try {
            jQuery(elem).filter("div").hide().fadeIn();
        } catch (e) {
            console.log(e);
        }
    }
    

    Related: I don’t recommend putting big javascript functions right inside your HTML. It clutters the HTML and mixes concern of view and view logic. I’d instead do something like this:

    <div id="wrapper" data-bind="template: {
        name:'theTemplate', 
        foreach: cars,
        beforeRemove: onRemovingCar,
        afterAdd: onAddedCar
    }"></div>
    
    <script type="text/javascript">
    
      var viewModel = {
         onRemovingCar: function(element) { 
              console.log(element);                                     
              console.log('try removing..');
              try {
                  jQuery(element).hide().remove();
              } catch (e) {
                  console.log(e);
              }
          },
          onAddedCar: function(element) { 
              console.log('try adding..');
              try {
                  jQuery(element).hide().fadeIn();
              } catch (e) {
                  console.log(e);
              }
          }
      };
    
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In knockoutjs 1.2.1 I could do: <div data-bind=template: {name: 'Bar', foreach: persons, templateOptions:{fooMode: true}
I need to name the <div>'s in a KnockoutJS template according to the position
If you do this: <ul data-bind=foreach: products> <li> <strong data-bind=text: name></strong> <em data-bind=if: manufacturer>
Let's say we have data as follows var data = { facets: [{ name
If I had a template in KnockoutJs which is a 'foreach' template, when I
So if I wanted to call a function with knockout's data-bind=click: ShowHide how would
I am new to KnockOut JS and I cannot find the reason for data-bind
I have a jQuery template, and I would like to use a KnockOutJS template
How do we populate data in the knockoutjs view model on the first call
I'm trying to use KnockoutJS to build an interactive dashboard with data bindings on

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.