Javascript - Simple Copy To Clipboard
Submitted by razormist on Saturday, August 4, 2018 - 19:16.
In this tutorial we will create a Simple Copy To Clipboard using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...
There you have it we successfully created a Simple Copy To Clipboard using javascript. I hope that this very simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
Getting started:
This is the link for the bootstrap that has been used for the layout of the calculator https://getbootstrap.com/.The Main Interface
This code contains the interface of the application. This code will render application and display the form. To create this just write these block of code inside the text editor and save this as index.html.- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- <link rel="stylesheet" type="text/css" href="css/snackbar.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-6">
- <pre style="white-space:normal;" id="copy">
- $(function(){
- $('#password').keyup(function(){
- var password = $('#password').val();
- if(password == ''){
- $('#progress').removeClass().addClass('progress-bar');
- $('#strength').html('');
- }
- else{
- var meter = checkStrength(password);
- $('#strength').html(meter);
- }
- });
- });
- </pre>
- </div>
- <div class="col-md-6">
- </div>
- </div>
- </body>
- </html>
Creating the Script
This code contains the script of the application. This code will copy the content from the targeted data, and store in the clipboard. To do this just copy write the code as shown below inside the text editor and save it as script.js.- function Copy(){
- var copyDoc = document.querySelector('#copy');
- var range = document.createRange();
- range.selectNode(copyDoc);
- window.getSelection().addRange(range);
- try
- {
- var successful = document.execCommand('copy');
- var msg = "Copy to clipboard";
- SnackBar(msg)
- }
- catch (err)
- {
- alert("Error: Copy!")
- }
- window.getSelection().removeAllRanges();
- }
- function SnackBar(msg) {
- var toast = document.getElementById("snackbar");
- toast.className = "show";
- document.getElementById("snackbar").innerHTML = msg;
- setTimeout(function(){ toast.className = toast.className.replace("show", ""); }, 3000);
- }
Add new comment
- 129 views