I am trying to add a "data-bind" attribute to a div using jQuery as follows:
var messageViewModel = {
data: ko.observable({
message: response.message,
sendDateFmted: response.sendDateFmted,
messageId: response.messageId
})
};
$("<div>",{
class:"messageToAndFromOtherMember"
}).data("bind", "template: { name: 'message-template', data: data }").appendTo("#messagesToAndFromOtherMember");
ko.applyBindings(messageViewModel);
"data-bind" is required by KnockoutJs. However all I get is this empty div:
<div class="messageToAndFromOtherMember"></div>
Notice there is no such attribute as data-bind
and therefore the div remains empty...
jQuery's .data()
stores the values in-memory and uses data-*
attributes for initialization. You may want to stick by setting it at element creation.
$("<div/>", {
class: "messageToAndFromOtherMember",
"data-bind": "template: { name: 'message-template', data: data }"
}).appendTo("#messageToAndFromOtherMember");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With