title: Conditionally add an element attribute in knockout.js
body: <p>The knockout.js library has an <a href="http://knockoutjs.com/documentation/attr-binding.html" rel="noreferrer"><code>"attr"</code> data binding</a> which allows you to dynamically change the value of an HTML element attribute (e.g. the "title").  However, in some cases, the attribute may or may not be needed depending on the corresponding observable on the bound object.  For example, if my model has a "title" observable I might want to set the "title" attribute if it is present (non-null) or skip the attribute entirely if it is not present (null).</p>
  
  <p>Does knockout provide any way to conditionally set an attribute?  (Ideally without conditionally rendering the entire element opening tag...)</p>
  
  <p><strong>[Note]</strong> This similarly named question was actually resolved by knockout's special handling of CSS classes and does not relate to this question (or its own title): <a href="https://stackoverflow.com/questions/11014336/how-to-conditionally-render-an-html-attribute-with-knockoutjs">How to conditionally render an css class with knockoutjs</a></p>