title: HTML-encoding lost when attribute read from input field
body: <p>I’m using JavaScript to pull a value out from a hidden field and display it in a textbox. The value in the hidden field is encoded.</p>
  
  <p>For example,</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;input id='hiddenId' type='hidden' value='chalk &amp;amp; cheese' /&gt;
  </code></pre>
  
  <p>gets pulled into</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;input type='text' value='chalk &amp;amp; cheese' /&gt;
  </code></pre>
  
  <p>via some jQuery to get the value from the hidden field (it’s at this point that I lose the encoding):</p>
  
  <pre class="lang-js prettyprint-override"><code>$('#hiddenId').attr('value')
  </code></pre>
  
  <p>The problem is that when I read <code>chalk &amp;amp; cheese</code> from the hidden field, JavaScript seems to lose the encoding. I do not want the value to be <code>chalk &amp; cheese</code>. I want the literal <code>amp;</code> to be retained.</p>
  
  <p>Is there a JavaScript library or a jQuery method that will HTML-encode a string?</p>