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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:44:25+00:00 2026-06-17T00:44:25+00:00

I have an observable array, bound to the list of contenteditable divs. I have

  • 0

I have an observable array, bound to the list of contenteditable divs. I have an ‘add’ button. On click I add an object to array and want to give focus to the corresponding div.

<ul id='list' data-bind="foreach: array">
  <li>
    <div contenteditable="true" data-bind="text: $data.text"></div>
  </li>
</ul>
<div id="add">+</div>

javascript

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

ko.applyBindings(viewModel, document.getElementById('list'));

document.getElementById('add').onclick = function (evt) {
  var newObject = {text : ''};
  viewModel.array.push(newObject);
  // give focus to the newly created div
};

It is possible to get the observable data having DOM element ko.dataFor(dom). How to get DOM by data?

http://jsfiddle.net/5rxdZ/

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-17T00:44:27+00:00Added an answer on June 17, 2026 at 12:44 am

    You can’t get the DOM element from the data itself. In this scenario though, you could use the hasfocus binding to move focus to the new element. Docs here: http://knockoutjs.com/documentation/hasfocus-binding.html

    Even just placing hasfocus: true on a new element will do the trick.

    Otherwise, if you don’t want the focus to be applied for the initially rendered elements, then you could pass in a flag for the newly created element like:

    <ul id='list' data-bind="foreach: array">
      <li>
        <div contenteditable="true" data-bind="hasfocus: $data.focused, text: $data.text"></div>
      </li>
    </ul>
    <div id="add">+</div>
    

    view model:

    var newObject = {text : '', focused: true};
    viewModel.array.push(newObject);
    

    Sample: http://jsfiddle.net/rniemeyer/jnHK8/

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

Sidebar

Related Questions

I have a an array of observable collections and I want to display all
I have an observable array that holds a list of people and a table
I have a Knockout.JS observable array that is the basis for a list: self.list_elements
I'm trying to add a new item to an observable array when a button
I have an observable array with var items= [ {image: /images/image1.jpg }, {image: /images/image2.jpg
I have a Knockout observable array that I wish to edit from within Javascript
I am new to Knockout.js, I have created an observable array and initialized with
I have an observable collection bound to a listbox in WPF. One of the
I have an observable array of Services that might be provided by a company
I have an observable array that contains a nested tree array. Every array item

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.