Hongmu Notes
Home Language Notes jquery clicks to copy the specified text and encapsulates multiple function calls
Language Notes JavaScript

jquery clicks to copy the specified text and encapsulates multiple function calls

jquery clicks to copy the specified text and encapsulates multiple function calls

https://www.4s5.cn/archives/903.htmlIn this article, I explained click-to-copy in detail, but my needs changed later!

I need multiple clicks, and the specified elements are all different!

So the edge needs to encapsulate the function!

Encapsulated code

function copyToClipboard(btnId, inputId) {
  $('#' + btnId).on('click', function() {
    var text = $('#' + inputId).val();
    var $temp = $('<textarea>');
    $('body').append($temp);
    $temp.val(text).select();
    document.execCommand('copy');
    $temp.remove();
    $("#copyModal .modal-body").text("成功!");
    $('#copyModal').modal('show'); // 显示 Bootstrap 弹窗提示
  });
}

Modal pop-up window with bootstrap!

You can save this function to your JavaScript file and call it wherever needed, for example:

copyToClipboard('copyBtn', 'inputToken');

code fix

The above code is bound to the click event, but when I quoted it, I thought about it and felt that some things were unnecessary, so I deleted the function.

function copyToClipboard(inputId, backupText, message) {
    var text = $('#' + inputId).length > 0 ? $('#' + inputId).val() : backupText;
    var $temp = $('<textarea>');
    $('body').append($temp);
    $temp.val(text).select();
    document.execCommand('copy');
    $temp.remove();
    $("#copyModal .modal-body").text(message || "复制成功");
    $('#copyModal').modal('show'); // 显示 Bootstrap 弹窗提示
}

This function only needs to fill in three parameters:

Specifies the contents of the copied element box

When the element box does not exist, the second parameter is copied.

After the copy is successful, the content of the prompt is optional.

Introduce examples

$(document).ready(function() {
  $('#copyBtn').on('click', function() {
    copyToClipboard("inputToken",'',"成功复制TOKENS,请仔细保存好,泄露之后,需要修改请联系管理员!");
  });
});

I'm really not good at front-end, so I apologize for my poor writing!

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

js move the mouse up to modify the style attribute

js move the mouse up to modify the style attribute Language Notes JavaScript

When the mouse hovers over the element, the color changes to red; when the mouse leaves the element, the color changes to black. To retrieve an element by its ID: `document.getElementById()`; Event handler for mouse hover: `onmouseover`; Event handler for mouse leave: `onmouseout`.<!DOCTYPE html><html lang= "en">...
👁 130

Recommended reading

Project Construction and Decoration Engineering Enterprise Website Template 0345

Project Construction and Decoration Engineering Enterprise Website Template 0345 Practical Collection Yiyou template

This EyouCMS template is ideal for construction and decoration engineering firms, featuring a professional architectural design style that effectively showcases decoration projects, construction case studies, design expertise, and corporate image. It helps decoration companies present their brand online and attract developers and property owners. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | Eyou...
👁 52
(Adaptive Mobile Version) PBootCMS Template for Intelligent License Plate Recognition Systems – Download Parking Lot System Website Source Code – 0407

(Adaptive Mobile Version) PBootCMS Template for Intelligent License Plate Recognition Systems – Download Parking Lot System Website Source Code – 0407 Practical Collection pbootcms Template

This is an adaptive mobile-friendly website template for the License Plate Recognition and Parking Lot Management System (PbootCMS). The design features a modern, professional aesthetic, making it ideal for showcasing license plate recognition systems, access control solutions, and smart parking solutions. It enables smart security companies to showcase their products online and attract property management and municipal government clients. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s...
👁 40
Floral Gardening – Planting & Harvesting Website Template 0346

Floral Gardening – Planting & Harvesting Website Template 0346 Practical Collection Yiyou template

An EyouCMS website template designed for the flower gardening and harvesting industry. Its fresh and natural design is ideal for showcasing floral products, gardening activities, harvesting experiences, and brand stories. This template helps gardening businesses showcase their products online and attract both home-based and leisure customers. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 47
(Adaptive mobile version) Hotel & Homestay Website PBootCMS Template – Hotel and Guest Room Website Source Code Download – 0408

(Adaptive mobile version) Hotel & Homestay Website PBootCMS Template – Hotel and Guest Room Website Source Code Download – 0408 Practical Collection pbootcms Template

An adaptive mobile-friendly PbootCMS website template designed for hotels, guesthouses, and guest rooms. Its warm and comfortable design is ideal for showcasing hotel environments, room types and facilities, as well as booking services. This template helps guesthouses or hotels effectively present their brand image online and attract guests to book accommodations. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles: Pbo...
👁 39
Sculpture Machine & Equipment Website Template 0347

Sculpture Machine & Equipment Website Template 0347 Practical Collection Yiyou template

This EyouCMS template is designed for the sculpture machine and mechanical equipment industry, featuring a professional industrial design style that effectively showcases sculpture machine products, mechanical equipment, technical specifications, and industrial applications. It enables machinery manufacturing enterprises to present their products online and attract industrial customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (...
👁 52
(Adaptive mobile version) Heavy Industry Machinery Website – PBootCMS Template; Excavator & Bulldozer Website Source Code Download – 0409

(Adaptive mobile version) Heavy Industry Machinery Website – PBootCMS Template; Excavator & Bulldozer Website Source Code Download – 0409 Practical Collection pbootcms Template

This adaptive mobile-friendly PbootCMS website template is designed for heavy-duty industrial machinery, excavators, and bulldozers. Its bold yellow design is ideal for showcasing large-scale construction machinery products and project cases. It helps heavy machinery enterprises effectively demonstrate their product capabilities online and attract clients in the infrastructure and mining sectors. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 54