
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to secretly copy to clipboard JavaScript function in Chrome and Firefox?
In this article we are going to try how to secretly copy a JavaScript function to the clipboard.
We use the copytext() method to secretly copy JavaScript function to the clipboard. These functions work on JavaScript console as well. To understand better let’s look into the examples one by one.
Example
Following is the example program we used copyText() method to copy text to clipboard using JavaScript function.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <input type="text" value="Copy me!" id="myInput"> <button onclick="copyText()">Copy text</button> <script> function copyText() { var copyText = document.getElementById("myInput"); copyText.select(); document.execCommand("copy"); // alert("Copied the text: " + copyText.value); } </script> </body> </html>
Adding an alert
We can also add an alert, using the alert() method, to the text copied by the copytext() method.
Example
Following is the example program to copy text to clipboard and an alert will be displayed after copying the text.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <input type="text" value="Copy me!" id="myInput"> <button onclick="copyText()">Copy text</button> <script> function copyText() { var copyText = document.getElementById("myInput"); copyText.select(); document.execCommand("copy"); alert("Copied the text: " + copyText.value); } </script> </body> </html>
Alert for the copied text reads −
Example
Following is the example program to copy text to clipboard using copyClipboard() function −
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <input type="text" value="Text" id="input"> <button onclick="copyClipboard()">Copy text Here!</button> <script> function copyClipboard() { var sampleText = document.getElementById("input"); sampleText.select(); sampleText.setSelectionRange(0, 99999) document.execCommand("copy"); alert("Copied the text: " + sampleText.value); document.write("Copied Text here:", sampleText.value); } </script> </body> </html>
- Related Articles
- How to enable JavaScript in Chrome, Firefox, and Safari?
- How to copy text to the clipboard with JavaScript?
- How to Delete Bookmarks in your Browser (Firefox, Chrome)
- How to find JavaScript function definition in Chrome?
- Creating ‘Copy to Clipboard’ feature on a web page with JavaScript
- How to copy text to the clipboard/pasteboard with Swift?
- How to call a JavaScript Function from Chrome Console?
- How to disable JavaScript in Firefox?
- Copy and paste to your clipboard using the pyperclip module in Python
- CSS Opacity in Firefox, Safari, Chrome, Opera
- Copy from clipboard using Python and Tkinter
- How to copy from clipboard using tkinter without displaying a window
- How to Automatically Copy a Cell to Clipboard with a Single Click in Excel?
- How to verify an XPath expression in Chrome Developers tool or Firefox's Firebug?
- Which is the best browser to use: Chrome, Explorer, or Firefox?

Advertisements