javascript - knockout data-bind syntax in js ( not HTML) -
i understand can create bindings using following syntax in dom:
<span id="namespan" data-bind="text: personname">
working example here:https://jsfiddle.net/m14mohda/
but can create such span element in js? i.e. using like:
createspans = function (){ var s = document.createelement('span') s.id = "namespan" s.data-bind ="text: personname" -----> ???? document.body.appendchild(s) }
the dom element api offers 2 ways of doing this. more general one, should work custom attribute using setattribute
method.
var s = document.createelement('span'); s.setattribute("someattribute", "somevalue"); s.setattribute("data-bind", "text: personname"); document.body.appendchild(s);
creates element
<span someattribute="somevalue" data-bind="text: personname"></span>
specifically attributes data- prefixed, specification includes shorthand property accessor dataset
.
var s = document.createelement('span'); s.dataset.someattribute = "somevalue"; // won't work expected s.dataset.bind = "text: personname"; document.body.appendchild(s);
which creates element
<span data-someattribute="somevalue" data-bind="text: personname"></span>
so can see dataset
takes data- prefix granted. data- attribute values can accessed , assigned through dataset
stripping data- part.
Comments
Post a Comment