Jump to content
Fórum Script Brasil
  • 0

Como copiar conteúdo de duas div's ao mesmo tempo?


diegoxlz

Question

É o seguinte, com o código abaixo só consigo escolher uma das divs, eu quero clicar no botão de copiar e copiar as duas divs, com um clique só...

<div id="markup">
  Text that can be copied or selected with cross browser support.
</div>
<br><br>
não copiar esse texto
<br><br>
<div id="markup2">
  Copiar o texto 2
</div>
<br><br>

<button id="botaodecopiar1">copiar!</button>

E o javascript
 

<script type="text/javascript">
        function selectElementContents(el) 
{
    // Copy textarea, pre, div, etc.
	if (document.body.createTextRange) {
        // IE 
        var textRange = document.body.createTextRange();
        textRange.moveToElementText(el);
        textRange.select();
        textRange.execCommand("Copy");     
    }
	else if (window.getSelection && document.createRange) {
        // non-IE
        var range = document.createRange();
        range.selectNodeContents(el);
        var sel = window.getSelection();
        sel.removeAllRanges();
        sel.addRange(range); 
        try {  
		    var successful = document.execCommand('copy');  
		    var msg = successful ? 'successful' : 'unsuccessful';  
		    console.log('Copy command was ' + msg);  
		} catch(err) {  
		    console.log('Oops, unable to copy');  
		} 
    }
} // end function selectElementContents(el) 

function make_copy_button(el)
{
	var botaodecopiar = document.getElementById('botaodecopiar1');
	botaodecopiar.onclick = function() { selectElementContents(el); };
	
	if (document.queryCommandSupported("copy") || parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2]) >= 42)
	{
		// Copy works with IE 4+, Chrome 42+, Firefox 41+, Opera 29+
		botaodecopiar.value = "Copy to Clipboard";
	}	
	else
	{
		// Select only for Safari and older Chrome, Firefox and Opera
		botaodecopiar.value = "Select All (then press CTRL+C to Copy)";
	}
}
var elem = document.getElementById("markup");
var elem2 = document.getElementById("markup2");
make_copy_button(elem2);
</script>

 

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.1k
    • Total Posts
      652k
×
×
  • Create New...