jQuery .data() does not update HTML5 data attributes

custom-data-attribute, jquery, knockout.js

Solution

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");

Problem

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...

Original source