Knockoutjs Attr Binding tips

KnockoutJS is a MVVC framework which is very handy and light weight compared to AngularJS.  This post is to share a question I have face when work with the Attr Binding.

in IE8/9 if you want to use the Attribute Binding, it is better to put a Quote the attribute such that it would be able to avoid some keyword conflicting issue. ie: the ‘for’ attribute of a Label tag will cause knockoutjs failed to parse the tag.

<input type="radio" name="radio" id="radio1">
 <label data-bind="attr: {for:id}, text:label"></label>
<script type="text/javascript">
 var viewModel = function(){
      var self = this;
      self.id = ko.observable("radio1");
      self.label ="Yes - click me to check the button";
 };
ko.applyBindings(viewModel);
</script>

Try this out http://fiddle.jshell.net/452ctauL/2/

it will not work in IE8/IE9, but will work in Chrome/Firefox/iOS8+/IE11+

Leave a Reply

Your email address will not be published. Required fields are marked *