How to copy text from a div to clipboard
JavaScript
function (container_id, as_html) {
/*
isEmpty = function (val) {
return (val === undefined || val == null || val.length <= 0) ? true : false;
};
*/
as_html = isEmpty(as_html) ? false : true;
//console.log('copyToClipboard: ' + container_id);
var elem = document.getElementById(container_id);
// create hidden text element, if it doesn't already exist
var targetId = "_hiddenCopyText_";
var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
var origSelectionStart, origSelectionEnd;
if (isInput) {
// can just use the original source element for the selection and copy
target = elem;
origSelectionStart = elem.selectionStart;
origSelectionEnd = elem.selectionEnd;
//console.log ('_is_input = true');
} else {
// must use a temporary form element for the selection and copy
//console.log ('_is_input = false, creating a textarea = ' + targetId);
target = document.getElementById(targetId);
if (!target) {
var target = document.createElement("textarea");
target.style.position = "absolute";
target.style.left = "-9999px";
target.style.top = "0";
target.id = targetId;
document.body.appendChild(target);
}
var content = $.trim(as_html ? $(elem).html() : $(elem).text());
$(target).text( content);
console.log ('text_content', content);
}
// select the content
var currentFocus = document.activeElement;
target.focus();
target.setSelectionRange(0, content.length);
// copy the selection
var succeed;
try {
succeed = document.execCommand("copy");
} catch (e) {
succeed = false;
}
// restore original focus
if (currentFocus && typeof currentFocus.focus === "function") {
currentFocus.focus();
}
if (isInput) {
// restore prior selection
elem.setSelectionRange(origSelectionStart, origSelectionEnd);
} else {
// clear temporary content
$(target).text('');
}
return succeed;
}
Also in JavaScript:
- Title
- how to find remainder in javascript
- Category
- JavaScript
- Title
- javascript Check if an element is a descendant of another
- Category
- JavaScript
- Title
- electron open new window
- Category
- JavaScript
- Title
- add dev dependency yarn
- Category
- JavaScript
- Title
- enter event in jquery
- Category
- JavaScript
- Title
- how the sort function works javascript
- Category
- JavaScript
- Title
- angular npm angular material
- Category
- JavaScript
- Title
- how to make a screen recording software with js
- Category
- JavaScript
- Title
- async await anonymous function
- Category
- JavaScript
- Title
- for of array javascript
- Category
- JavaScript
- Title
- javascript check if number
- Category
- JavaScript
- Title
- comment obtenir l'élément parent javascript
- Category
- JavaScript
- Title
- how to pass custom regex in jquery validation
- Category
- JavaScript
- Title
- capitalize all letters jquery
- Category
- JavaScript
- Title
- Angularjs datetime
- Category
- JavaScript
- Title
- add element to array javascript
- Category
- JavaScript
- Title
- date picker javascript not working
- Category
- JavaScript
- Title
- how to use style in react js
- Category
- JavaScript
- Title
- file input disable open file picker javascript
- Category
- JavaScript
- Title
- hasownproperty.call
- Category
- JavaScript
- Title
- interaction js drag anddrop
- Category
- JavaScript
- Title
- faker js
- Category
- JavaScript
- Title
- how to manage a db connection in javascript
- Category
- JavaScript
- Title
- cdn react
- Category
- JavaScript
- Title
- array javascript django
- Category
- JavaScript
- Title
- discord.js how to go back a file
- Category
- JavaScript
- Title
- javascript add minutes to date
- Category
- JavaScript
- Title
- create javascript button
- Category
- JavaScript
- Title
- isogram
- Category
- JavaScript
- Title
- check undefined object javascript one liner set to emtpy
- Category
- JavaScript
- Title
- create dice game javascript
- Category
- JavaScript
- Title
- angular 6 reload current page
- Category
- JavaScript
- Title
- discord js get badge user
- Category
- JavaScript
- Title
- $.get jquery return value
- Category
- JavaScript
- Title
- edit onclick event
- Category
- JavaScript
- Title
- how to get selected row index of table in javascript
- Category
- JavaScript
- Title
- angular run with proxy
- Category
- JavaScript
- Title
- exponential javascript
- Category
- JavaScript
- Title
- how to capitalize a letter based on an index in javascript
- Category
- JavaScript
- Title
- window location in react
- Category
- JavaScript
- Title
- add a route to a buttoin in angular
- Category
- JavaScript
- Title
- document load javascript
- Category
- JavaScript
- Title
- how to open a new html page on button click in javascript
- Category
- JavaScript
- Title
- document delete element
- Category
- JavaScript
- Title
- ejs current year
- Category
- JavaScript
- Title
- dimensions react native
- Category
- JavaScript
- Title
- injected stylesheet remove
- Category
- JavaScript
- Title
- foreach object javascript
- Category
- JavaScript
- Title
- https package node post request
- Category
- JavaScript
- Title
- javascript circular evaluation
- Category
- JavaScript
- Title
- dynamic forms in react
- Category
- JavaScript
- Title
- how to get href value of anchor tag in jquery in list
- Category
- JavaScript
- Title
- express bodyparser deprecated
- Category
- JavaScript
- Title
- delete last element of array js
- Category
- JavaScript
- Title
- add a slash to string in javascript
- Category
- JavaScript
- Title
- events in javascript
- Category
- JavaScript
- Title
- can i use redux connect without react
- Category
- JavaScript
- Title
- How to remove options from selectlist in javascript?
- Category
- JavaScript
- Title
- greater than or equal to javascript
- Category
- JavaScript
- Title
- how to get nth fibonacci javascript
- Category
- JavaScript
- Title
- draw rectangle on javascript canvas
- Category
- JavaScript
- Title
- get value of checked radio button jquery
- Category
- JavaScript
- Title
- get query parameters in node.js
- Category
- JavaScript
- Title
- get value from textbox in vanilla javascript
- Category
- JavaScript
- Title
- how to remove item from array javascript
- Category
- JavaScript
- Title
- empty array js
- Category
- JavaScript
- Title
- forEach
- Category
- JavaScript
- Title
- find particular object from array in js
- Category
- JavaScript
- Title
- hooks in react
- Category
- JavaScript
- Title
- charat javascript
- Category
- JavaScript
- Title
- async reduce javascript
- Category
- JavaScript
- Title
- how to access all images in a document javascript
- Category
- JavaScript
- Title
- javascript check string lenght
- Category
- JavaScript
- Title
- add val to array jquery
- Category
- JavaScript
- Title
- create a json object in javascript
- Category
- JavaScript
- Title
- adding border in react native
- Category
- JavaScript
- Title
- angular email validation
- Category
- JavaScript
- Title
- function inside a class component react
- Category
- JavaScript
- Title
- disable mixed content via javascript
- Category
- JavaScript
- Title
- * http://sizzlejs.com/
- Category
- JavaScript
- Title
- check items in array javascript
- Category
- JavaScript
- Title
- extend react.component
- Category
- JavaScript
- Title
- A cookie associated with a cross-site resource at http://placeholder.com/ was set without the `SameSite` attribute.
- Category
- JavaScript
- Title
- clone an object in javascript
- Category
- JavaScript
- Title
- Javascript change element class
- Category
- JavaScript
- Title
- array length javascript
- Category
- JavaScript
- Title
- get value onChange from mat-select angular
- Category
- JavaScript
- Title
- insert item into array specific index javascript
- Category
- JavaScript
- Title
- javascript check if variable exists
- Category
- JavaScript
- Title
- check if message mentions users discord js
- Category
- JavaScript
- Title
- fs create or edit file
- Category
- JavaScript
- Title
- how to detect a button click in javascript
- Category
- JavaScript
- Title
- how to create 3d gallery using javascript
- Category
- JavaScript
- Title
- find last element with class jquery
- Category
- JavaScript
- Title
- how to read json file in python
- Category
- JavaScript
- Title
- convert milliseconds to minutes and seconds javascript
- Category
- JavaScript
- Title
- datatables server side
- Category
- JavaScript
- Title
- javascrip check if string contains substring
- Category
- JavaScript
- Title
- How to uninstall npm modules in node js?
- Category
- JavaScript
- Title
- Javascript case insensitive string comparison
- Category
- JavaScript