Skip to main content

Copy text or html to clipboard js php

How to copy text or HTML to clipboard using JS/jQuery

Today we learn how to copy texts and html into the clipboard by using js, we can copy content on button click or any event through JS.


Here are the few simple methods for it. Please follow the below methods


1) Copy data-attribute value to clipboard

  If you have an html element like a button with data value and you want to copy value on button click then use below function

HTML CODE

<a class="cls_copy_pg_action copyAction copy-action-btn" data-value="THIS TEXT WILL BE COPIED"> <i class="far fa-copy"></i> Copy</a>


JQUERY CODE

$(document).on("click", ".copy-action-btn", function() { 
      var trigger = $(this);
      $(".copy-action-btn").removeClass("text-success");
      var $tempElement = $("<input>");
        $("body").append($tempElement);
        var copyType = $(this).data("value");
        $tempElement.val(copyType).select();
        document.execCommand("Copy");
        $tempElement.remove();
        $(trigger).addClass("text-success");

  });
 
 
The above code will copy the data into clipboard. 

2) Copy INPUT field value on button click

HTML CODE
 
<input type="text"  value="THIS TEXT WILL BE COPIED" id="idInputField">
<button class="clsCopyBtn" onclick="copyFunction()">Copy Action</button>
 
 
JQUERY CODE
 
 function copyFunction() 
 {
      var trigger = $(this);
      $(".copy-action-btn").removeClass("text-success");
      var $tempElement = $("<input>");
      $("body").append($tempElement);
      var copyType = $("#idInputField").val();
      $tempElement.val(copyType).select();
      document.execCommand("Copy");
      $tempElement.remove();
      $(trigger).addClass("text-success");
  };
 
 

3) Copy to the clipboard using PURE JAVASCRIPT

  If you dont want to use jQuery then you can do copy work with javascript too. Please 
 use below function
 
function copyFunction() {
  var copyInputContent = document.getElementById("idInputField");
  copyInputContent.select();
  document.execCommand("copy");
}  
 

4) Copy HTML of a HTML element

 If you want to copy html of a HTML element like a div then you dont need to use clipboard
. you can get html of element and save into a variable


 
function copyFunction() 
 {
      var trigger = $(this);
      $(".copy-action-btn").removeClass("text-success");
      var $tempElement = $("<input>");
      $("body").append($tempElement);
      var copyType = $(".text-pages-heading").html();
      $tempElement.val(copyType).select();
      document.execCommand("Copy");
      $tempElement.remove();
      $(trigger).addClass("text-success");
  };

Comments

  1. Awsssmmmm post bro keep it up great work
    https://gajabwap.blogspot.com

    ReplyDelete
  2. Website Speed Optimization is using the strategies and techniques to speed up your site & make it faster to load in the web browsers. If your website loads slowly then you will lose your site rankings in search engine results. It requires a lot of efforts & tweaks to optimize your website speed. I have listed over 5 website speed optimization tips to increase your website speed. Moreover, you can use these techniques for both WordPress & Non-WordPress.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Your Affiliate Money Making Machine is waiting -

    Plus, earning money online using it is as easy as 1..2..3!

    Here are the steps to make it work...

    STEP 1. Choose affiliate products the system will push
    STEP 2. Add push button traffic (this ONLY takes 2 minutes)
    STEP 3. See how the affiliate system explode your list and sell your affiliate products on it's own!

    Do you want to start making money???

    Click here to launch the system

    ReplyDelete
  5. Your article is very good. I'm following you.I understand exactly what you mean today. I had been to many blogs before but could not understand it. So I was very impressed when I read your article. Thank you very much for this.


    hi guys, if you want earn money in one day, so click here.


    Online Friendship Club
    Dating friendship
    Free Online Dating Site
    Online Dating
    Matchmaker
    Dating friendship

    ReplyDelete
  6. Thank you for your post, I look for such article along time, today i find it finally. this post give me lots of advise it is very useful for me.
    web designing course in chennai

    ReplyDelete
  7. How To Clone Or Duplicate Html Element Using Jquery

    Learn how to clone or duplicate html element using jquery.

    For More Info:- How To Clone Or Duplicate Html Element Using Jquery

    ReplyDelete
  8. WanlitOgrat-ne Brian Thao click
    smithasoblu

    ReplyDelete

Post a Comment