jquery - Knockoutjs databind creates more than one element -


i trying learn knockoutjs. reading write url http://knockoutjs.com/documentation/foreach-binding.html

i try run code above url when click button more 1 li element getting created, should create one.

here html code

<ul data-bind="foreach: { data: myitems, afteradd: yellowfadein }">     <li data-bind="text: $data"></li> </ul>  <button data-bind="click: additem">add</button> 

js code

ko.applybindings({         myitems: ko.observablearray([ 'a', 'b', 'c' ]),         yellowfadein: function(element, index, data) {              $(element).filter("li")                       .animate({ backgroundcolor: 'yellow' }, 200)                       .animate({ backgroundcolor: 'white' }, 800);         },         additem: function() {this.myitems.push('new item'); }     }); 

the above code posted in knockout js tutorial page not working expected. have checked jquery animate function being called not changing bg color. wrong in code?

the code posted needs jquery run part:

$(element).filter("li").animate({ backgroundcolor: 'yellow' }, 200).animate({ backgroundcolor: 'white' }, 800); 

if don't have jquery yellowfadein function fail , apparently knockout tries re-apply previous attempts @ every click.

see fiddle edited: http://jsfiddle.net/jkrxv8e8/2/

note jquery documentation animate says background-color property cannot animated unless jquery.color plugin used.

here fiddle relevant plugin added

http://jsfiddle.net/jkrxv8e8/3/


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -