javascript - Selecting element with id attribute data bound via Knockout -
why element undefined when try select element using jquery:
<ul data-bind="attr: {id: panelid}"></ul> panelid defined property in knockoutjs viewmodel:
var vm = function () { var self = this, date = new date(); self.panelid = "panel-" + date.gettime(); $("#"+self.panelid).dosomthing(); // element undefined } inspecting page in chrome developer tool, can see id assigned
<ul data-bind ... id="panel-1368039734501"</ul> the panelid property doesn't have observable. tried make observable same result.
anyone?
the issue id attribute applied in ko.applybindings call, try find element id before that.
to avoid that, this:
var vm = function () { var self = this, date = new date(); self.panelid = "panel-" + date.gettime(); } var vm = new vm(); ko.applybindings(vm); // adds id attribute $("#"+vm.panelid).dosomething(); // needs after ko.applybindings
Comments
Post a Comment