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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:53:03+00:00 2026-05-23T04:53:03+00:00

I’m binding an observableArray to a template that generates a table with a <tr>

  • 0

I’m binding an observableArray to a template that generates a table with a <tr> for each item in the array. The idea is that, when the user enters text in the last row, the page automatically adds another row; it’s a dynamically-expanding list of entries.

This all works fine, but the keyboard focus is lost when this happens. I posted the broken sample in this fiddle, which contains the following:

function ingredientViewModel(name, qty, note) {
    this.name = ko.observable(name);
    this.quantity = qty;
    this.note = note;
}

var viewModel = {
    ingredients: ko.observableArray([]),
};

// When last ingredient's name changes,
//   Add a new row to the list
//   Update the global subscription to point to the new item


function lastIngredientNameChanged(newValue) {
    var currentfocus = document.activeElement;
    if (newValue != '') {
        lastIngredientSubscription.dispose();
        viewModel.ingredients.push(new ingredientViewModel('', '', ''));
        lastIngredientSubscription = viewModel.ingredients()[viewModel.ingredients().length - 1].name.subscribe(lastIngredientNameChanged);
    }
    currentfocus.focus();
}

// Set up initial entries
viewModel.ingredients.push(new ingredientViewModel('', '', ''));
var lastIngredientSubscription = viewModel.ingredients()[viewModel.ingredients().length - 1].name.subscribe(lastIngredientNameChanged);

ko.applyBindings(viewModel);

And this View code:

<script type="text/html" id="ingredientTemplate">
    < table id = "ingredienttable" > < colgroup > < col width = "200" / > < col width = "40" / > < col / > < /colgroup>
        <thead><tr>
            <td>Name</td > < td > Amount < /td>
            <td>Note</td > < /tr></thead > < tbody > {
        {
            each ingredients
        }
    } < tr class = "ingrediententry" > < td > < input class = "ingredientautocomplete"
    data - bind = "value: name, valueUpdate: 'afterkeydown'" / > < /td>
                <td><input data-bind="value: quantity" / > < /td>
                <td><input data-bind="value: note" / > < /td>
            </tr > {
        {
            /each}}
        </tbody > < /table>
</script>
<div data-bind="template: 'ingredientTemplate'"></div>

Any ideas?

  • 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-23T04:53:04+00:00Added an answer on May 23, 2026 at 4:53 am

    The issue is that when you use {{each}} and the observableArray that you are looping on changes, then the entire template is re-rendered. So, your currentfocus element is actually gone.

    What you can do is switch to using the template binding’s foreach option, which will only re-render changed rows in your template. Your HTML would look like:

    <script type="text/html" id="ingredientTemplate">
        <table id="ingredienttable">
            <colgroup>
                <col width="200"/>
                <col width="40"/>
                <col/>
            </colgroup>
            <thead><tr>
                <td>Name</td>
                <td>Amount</td>
                <td>Note</td>
                </tr></thead>
            <tbody data-bind="template: { name: 'rowTmpl', foreach: ingredients }">
            </tbody>
        </table>
    </script>
    
    <script id="rowTmpl" type="text/html">
        <tr class="ingrediententry">
            <td><input class="ingredientautocomplete" data-bind="value: name, valueUpdate: 'afterkeydown'" /></td>
            <td><input data-bind="value: quantity" /></td>
            <td><input data-bind="value: note" /></td>
        </tr>
    </script>
    
    <div data-bind="template: 'ingredientTemplate'"></div>
    

    Sample here: http://jsfiddle.net/rniemeyer/T9UP6/

    If you do it this way, then you don’t even have to track the currentfocus, it will just remain.

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

Sidebar

Related Questions

No related questions found

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.