title: How to pass a string which contains HTML to an onClick event?
body: <p>I'm creating an  element in my c# code, which contains a onclick event that copies the string parameter to the clipboard.</p>
  
  <p>It works fine, the problem is when I have to pass an parameter that is a HTML code, with <br /> and  tags. It breaks my page, because of the quotes (') that may appear (like style and colspan tags).</p>
  
  <p>The code I'm using is this:</p>
  
  <pre><code>string comment = row.Cells[1].Text;
  
  Cells[0].Text += "&lt;br/&gt;&lt;a href='javascript:' onclick='javascript:CopyComment(" + comment + ")'&gt;COPY&lt;/a&gt;";
  </code></pre>
  
  <p>How can I pass a string with HTML as parameter and not break my page?</p>
  
  <p>EDIT 1:</p>
  
  <p>This is the HTML Code I'm passing as parameter:</p>
  
  <pre><code>&gt; "&lt;span style='color:#FF0000;'&gt;&lt;b&gt;COTAÇÃO&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;\r\n&lt;br
  &gt; /&gt;\r\n&lt;br /&gt;\r\nAssunto: Cotação / Assistência: 425595 / Placa:
  &gt; EAL8426&lt;br /&gt;\r\n&lt;br /&gt;\r\nSegue conforme solicitado:&lt;br /&gt;\r\n&lt;br
  &gt; /&gt;\r\n&lt;span
  &gt; style='text-decoration:underline;'&gt;&lt;b&gt;ORIGEM:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;\r\nRua
  &gt; Glauber Rocha, 39 - Jardim Alzira, São Paulo - SP, 03986-270, Brasil,
  &gt; 39&lt;br /&gt;\r\n&lt;br /&gt;\r\n&lt;span
  &gt; style='text-decoration:underline;'&gt;&lt;b&gt;DESTINO:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;\r\nR.
  &gt; Conselheiro Saraiva, 36 - Santana, São Paulo - SP, Brasil, 36&lt;br
  &gt; /&gt;\r\n&lt;br /&gt;\r\n&lt;br /&gt;\r\n&lt;b&gt;Cotação 1&lt;/b&gt;&lt;br /&gt;\r\nTempo de Chegada:
  &gt; 50&lt;br /&gt;\r\n&lt;br
  &gt; /&gt;\r\n&lt;table&gt;\r\n&lt;tr&gt;\r\n&lt;td&gt;Tarifa&lt;/td&gt;&lt;td&gt;Valor&lt;/td&gt;&lt;td&gt;Quantidade&lt;/td&gt;&lt;td&gt;Total&lt;/td&gt;\r\n&lt;/tr&gt;\r\n&lt;tr&gt;\r\n&lt;td&gt;Saída&lt;/td&gt;&lt;td&gt;R$
  &gt; 99,00&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;R$ 99,00&lt;/td&gt;\r\n&lt;/tr&gt;\r\n&lt;tr&gt;\r\n&lt;td&gt;KM
  &gt; (Excedente)&lt;/td&gt;&lt;td&gt;R$ 1,50&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;R$
  &gt; 15,00&lt;/td&gt;\r\n&lt;/tr&gt;\r\n&lt;tr&gt;\r\n&lt;td&gt;Pedágio&lt;/td&gt;&lt;td&gt;R$
  &gt; 20,00&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;R$ 20,00&lt;/td&gt;\r\n&lt;/tr&gt;\r\n&lt;tr&gt;\r\n&lt;td&gt;Horas
  &gt; Trabalhadas&lt;/td&gt;&lt;td&gt;R$ 46,00&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;R$
  &gt; 92,00&lt;/td&gt;\r\n&lt;/tr&gt;\r\n&lt;tr&gt;\r\n&lt;td&gt;KM (Deslocamento)&lt;/td&gt;&lt;td&gt;R$
  &gt; 1,50&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;R$
  &gt; 15,00&lt;/td&gt;\r\n&lt;/tr&gt;\r\n&lt;tr&gt;\r\n&lt;td&gt;Destombamento&lt;/td&gt;&lt;td&gt;R$
  &gt; 125,00&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;R$ 125,00&lt;/td&gt;\r\n&lt;/tr&gt;\r\n&lt;tr&gt;\r\n&lt;td
  &gt; colspan='2'&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;Total&lt;/td&gt;&lt;td&gt;366,00&lt;/td&gt;\r\n&lt;/tr&gt;\r\n&lt;/table&gt;\r\n&lt;br
  &gt; /&gt;\r\n\r\n&lt;br /&gt;\r\nNo aguardo&lt;br /&gt;\r\n&lt;br /&gt;\r\nAtenciosamente\r\n"
  </code></pre>
  
  <p>this is my javascript function that copies the text (with the HTML format) to the clipboard</p>
  
  <pre><code>function CopiarComentario(html) {
      var tmpEl;
      tmpEl = document.createElement("div");
  
      // since we remove the element immediately we'd actually not have to style it - but IE 11 prompts us to confirm the clipboard interaction and until you click the confirm button, the element would show. so: still extra stuff for IE, as usual.
      tmpEl.style.opacity = 0;
      tmpEl.style.position = "absolute";
      tmpEl.style.pointerEvents = "none";
      tmpEl.style.zIndex = -1;
  
      // fill it with your HTML
      tmpEl.innerHTML = decodeHtml(html);
  
      // append the temporary node to the DOM
      document.body.appendChild(tmpEl);
  
      // select the newly added node
      var range = document.createRange();
      range.selectNode(tmpEl);
      window.getSelection().addRange(range);
  
      // copy
      document.execCommand("copy");
  
      // and remove the element immediately
      document.body.removeChild(tmpEl);
  }
  
  function decodeHtml(html) {
      var txt = document.createElement("textarea");
      txt.innerHTML = html;
      return txt.value;
  }
  </code></pre>