What is the best way to make hover text that also copies when you highlight the main text?

Currently, I am utilizing a basic bootstrap tooltip for creating tables with jQuery. However, I require a specific functionality that the standard bootstrap tooltip does not provide.

I need the ability to copy the base text and have it appear as "base text - hover text" when pasted into Excel. Since having two columns would make the table too large, I am exploring alternative solutions.

<a href="#" data-toggle="tooltip" title="hover text">base text</a>
$(document).ready(function(){
   $("#query").click(function() {   

    $.ajax({
            method: "GET",
            url: $('#dns').val()
        })
        .done(function(data) {
            if (data.Success == true) {
//This is where my table with tooltips is generated
                parseResults(data.Results);                 
            } else {
                $('.dnsError').show();
            }

            $('[data-toggle="tooltip"]').on('copy', function(event) {
                event.preventDefault();
                console.log(this.title);
                copyToClipboard(this.innerText + " - " + this.getAttribute('data-original-title'));
              }).tooltip();
        })
        });
});

function copyToClipboard(text) {
      var $temp = $("<input>");
      $("body").append($temp);
      $temp.val(text).select();
      document.execCommand("copy");
      $temp.remove();
    }

Answer №1

I incorporated a modified implementation of this in order to facilitate text copying to the clipboard.

It is crucial to retrieve the data-original-title instead of the regular title as Bootstrap tends to modify it.

function copyToClipboard(text) {
  var $temp = $("<input>");
  $("body").append($temp);
  $temp.val(text).select();
  document.execCommand("copy");
  $temp.remove();
}

$(document).ready(function() {
  $('[data-toggle="tooltip"]').on('copy', function(event) {
    event.preventDefault();
    console.log(this.title);
    copyToClipboard(this.innerText + " - " + this.getAttribute('data-original-title'));
  }).tooltip();
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://unpkg.com/popper.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>

<a href="#" data-toggle="tooltip" title="hover text">base text</a>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Switch between divs based on the current selection

var header = $("#accordion"); $.each(data, function () { header.append("<a id='Headanchor' href='javascript:toggleDiv($(this));'>" + this.LongName + "</a>" + "<br />", "&l ...

Bringing in typefaces to enhance your email signature

I am trying to design a unique email signature with a custom Adobe font. My email account is hosted by Strato, a German mail server. However, when I attempted to insert the signature using Apple Mail, I realized that the CSS <style> part was not bein ...

"Eliminate any inline styling from a string element with the power of JavaScript/j

If I have the following string: let sentence = '<h1 style="lots of class"> </h1><h2> <p style="bunch of class"> </p> <p style="bunch of class"> </p></h2>'; This string contains multiple elements a ...

Using a for-each loop to wait for the completion of an Ajax process

for (var i = 0; i < theTagNames.length; i++) { var url = baseURL + "?" + "jsoncallback=?" + "&method=flickr.photosets.getPhotos" + "&format=json" + "&api_key=" + api + "&photoset_id=" + theTagN ...

Why does tagging P trigger popups in the DIV when the DIV contains another DIV?

JSBIN HTML <p> <div>123</div> </p> CSS p div{ background:#f00; } Encountering an odd issue here. When I input the HTML structure as shown below: <p></p><div>123</div> The CSS code fails to produce ...

Styling with CSS: Enhancing list items with separators

Is there a way to include a separator image in my list using CSS? li { list-style: none; background-image: url("SlicingImage/button_unselect.png"); height: 53px; width: 180px; } This is the code for the separator image: url("SlicingImage ...

Step-by-step guide on using JQuery to listen to an event and dynamically add a p element after a button, with the feature to remove it on click

I need to implement a functionality where clicking a button will add a new paragraph after the button, and then be able to remove that paragraph by clicking on any word within it. Any guidance on how to achieve this would be highly appreciated. Thank you! ...

Removing an MySQL database using a PHP HTML button

While attempting to remove a row from my MySQL PHPMyAdmin database using a button in AssetApprovalForm.php, I encountered an issue. Upon clicking the decline button in AssetApprovalForm.php, nothing happens. Click here for the image of AssetApprovalForm.p ...

The PHP code for displaying a button with an onclick event may experience functionality issues

I've attempted to display the button using echo, but for some reason the link isn't functioning properly. I believe there may be an issue with the href link, could you please assist? echo "<input class='button_normal' type='but ...

The SubMenu icon in Android disappears when creating a menu dynamically

I'm currently working on creating a dynamic menu list, and I've managed to create the menu options successfully. However, every time I run my code, the icon for the main item disappears. Below is a snippet of my code: private static final int SU ...

Verify Session Cookies through JSONP requests

I've developed a bookmark that pulls all images from a webpage upon clicking and sends the image's src back to another server using JSONP. Challenge: The remote server must validate session authentication cookies to confirm that the user sending ...

Overflowing HTML Table Spills Over into Neighboring Div

Is there a way to make the data in a table that spans across multiple pages overflow into a separate div on the initial page without using JavaScript since I am generating it as a PDF? See the images below for reference. Current layout: https://i.stack.im ...

Selecting the appropriate technology or library for incorporating user-defined text along a designated path within established areas

I am currently developing an admin dashboard with CodeIgniter 2 that allows the admin to upload custom images, particularly ones with blank spaces for text overlay. The goal is to enable regular users to add their desired text in specific areas defined by ...

The Jquery code encountered an issue where it was unable to access the property 'length' of an undefined

My goal is to submit a form using jQuery and AJAX, which includes file upload functionality. The challenge I'm facing is that the forms are dynamically created, so I need to identify which form was clicked and retrieve its input values. $(document).r ...

What is causing the radio button's background color not to change after it is selected?

I've been searching and exploring various solutions, but I'm encountering some difficulties with the following scenario: There are a few restrictions: Cannot utilize Javascript or Jquery. Must be achieved using pure CSS. My objective is to chan ...

Sleek descending motion effect

I have created a simple function, but it is not animating smoothly and seems to lag... I am using it for a div sized at 1600x700 pixels on page load $(document).ready(function(){ $('#slider').slideDown(500); }); Is there any way to ensure s ...

Tips on Initiating a Phone Call using a Button Click within a Recyclerview

When using the method ACTION_CALL, I encountered a problem. Clicking the button on the first card view causes the app to call the phone number 11111111. However, clicking the button on the second card view also calls the same number 1111111. What I want i ...

The div smoothly descended from the top of the page to the center under the control of jQuery

I am trying to implement a feature where a div slides down from the top of the page to the center when a button is clicked. However, my current code seems to be causing the div to slide from the bottom instead of the top. Ideally, I want the div to slide ...

Change the z-index of divs when clicked

When a button is clicked, I want to iterate through a group of absolutely positioned children divs with varying z-indexes. The goal is for the z-index of the currently visible div to decrease on each click, creating a looping effect where only one div is v ...

Having trouble using Selenium for Chrome to locate an element in an HTML file using C#?

I am currently utilizing Selenium to locate elements within an HTML document that is being displayed in the Chrome browser. Unfortunately, the HTML code is not created by me and appears to have numerous issues. I do not have the ability to modify how it is ...