Note. This solution will work in IE10 +, Chrome 43+, Opera 29+ and Firefox 41+. Not for Safari!
You are most on the way, you just need select() <textarea> , and then use document.execCommand('copy') to load it to the clipboard.
var copyTextarea = document.querySelector('.auto'); $('button').click(function(element) { var thelist = $('#someList').html(); thelist = thelist.replace(/\s+<li>/g, ''); thelist = thelist.replace(/<\/?li>/g, '\r'); $('.auto').val(thelist); copyTextarea.select(); document.execCommand('copy'); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button>Copy List</button> <div class="listing"> <ul id="someList"> <li>1 million</li> <li>Monday</li> <li>Something</li> <li>Foobar</li> <li>1tsp blah</li> </ul> </div> <textarea class="auto"></textarea>
This requires <textarea> , but you can hide it with CSS. It will not be as simple as display:none , as this will disqualify him from being able to choose. You can install it from the screen or follow the detailed tip in This answer to make it invisible.
Additional spacing is due to indent your HTML, you can get rid of it by using String.prototype.trim() , but ideally I would instead build your list, passing DOM instead of using Regex.
Johnb source share