1

I know that there are a million posts on how to copy a javascript variable into a clipboard. I'm partial to this approach:

window.prompt("Copy to clipboard: Ctrl+C, Enter", output);

However, I'm wondering how I can get a variable that contains bold. My desired result is that I can paste the variable (into word) and have a portion of it bolded. I'm trying to set up up the variable like this:

var output = "something <b>bold section</b> something else";

I don't want to export that literally with the HTML, I want to export it as though I selected and then copied the following:

something bold section something else

How can I best achieve this result?

TiredofGoogling
  • 197
  • 1
  • 2
  • 9
  • [somehow related](http://stackoverflow.com/questions/17470817/format-the-text-in-javascript-alert-box) – Kaiido Nov 05 '15 at 18:53

1 Answers1

0

I think you can't style the prompt window.

But a possible way, for recent browsers (IE9+), is to use the Selection API instead of the prompt() trick :

btn.onclick = function() {
  // create a modal
  var p = document.createElement('p');
  p.innerHTML = "something <b>bold section</b> something else";
  document.body.appendChild(p);
  // create a new range of our message
  var range = document.createRange();
  // get the textNodes
  var nodes = p.childNodes;
  // start our range at first node
  range.setStart(nodes[0], 0);
  // end it at last
  range.setEnd(nodes[nodes.length - 1], nodes[nodes.length - 1].length);
  // create a Selection object
  var sel = getSelection();
  // remove existing ranges
  sel.removeAllRanges()
  // set our new one
  sel.addRange(range);
}
<button id="btn">show the text to copy</button>
Community
  • 1
  • 1
Kaiido
  • 123,334
  • 13
  • 219
  • 285