Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
178
rated 0 times [  185] [ 7]  / answers: 1 / hits: 64797  / 12 Years ago, sat, june 16, 2012, 12:00:00

How is it possible to make knockout data-bind work on dynamically generated elements? For example, I insert a simple html select menu inside a div and want to populate options using the knockout options binding. This is what my code looks like:



$('#menu').html('<select name=list data-bind=options: listItems></select>');


but this method doesn't work. Any ideas?


More From » jquery

 Answers
5

If you add this element on the fly after you have bound your viewmodel it will not be in the viewmodel and won't update. You can do one of two things.




  1. Add the element to the DOM and re-bind it by calling ko.applyBindings(); again

  2. OR add the list to the DOM from the beginning and leave the options collection in your viewmodel empty. Knockout won't render it until you add elements to options on the fly later.


[#84858] Friday, June 15, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brandensebastiand

Total Points: 323
Total Questions: 115
Total Answers: 106

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;