Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tooltip on click of a button

Tags:

I'm using clipboard.js to copy some text from a textarea, and that's working fine, but I want to show a tooltip saying "Copied!" if it was successfully copied like they do in the example given on their website.

Here's an example of it working without showing a tooltip: https://jsfiddle.net/5j50jnhj/

like image 319
user5368737 Avatar asked Jun 13 '16 20:06

user5368737


People also ask

How do I enable tooltip on click?

You will have to create a tooltip using HTML/CSS, bootstrap has a built in one. Then you can hide/show it depending on success/failure of the copying.

How do I add a tooltip to a tag?

Usage. Via data attributes − To add a tooltip, add data-toggle = "tooltip" to an anchor tag. The title of the anchor will be the text of a tooltip. By default, tooltip is set to top by the plugin.

How do I make hover tooltip?

HTML: Use a container element (like <div>) and add the "tooltip" class to it. When the user mouse over this <div>, it will show the tooltip text. The tooltip text is placed inside an inline element (like <span>) with class="tooltiptext" .

How do I add a tooltip to a button?

Under Action you have: Type, Bookmark, and Tooltip which is usually blank (the default CTRL-Click...). Put your tooltip into the tooltip box and it will show when you hover over the button. 10-05-2019 02:26 PM

How to display tooltip when hover over a button?

Put your tooltip into the tooltip box and it will show when you hover over the button. 10-05-2019 02:26 PM It works like a charm, thank you so much for the tip!

How to create a tooltip in Bootstrap?

We are going to learn how to create a tooltip and how to position the tooltip at different positions. Step 1: Include all the bootstrap libraries in the head section of your code. Step 2: To implement the tooltip in the button control of HTML, apply data-toggle property as shown below:

How to set a position to tool tipped text using CSS?

One can set a position to that tool tipped text by using CSS, which helps us to define some style and position to our tooltip. Using a tooltip to our WebPages is helps us do more interaction with the user because it gives short information of included elements. HTML uses a method that defines tooltip by using the link with a title attribute.


2 Answers

Clipboard.js creator here. So Clipboard.js is not opinionated about user feedback which means it doesn't come with a tooltip solution.

But here's an example of how you can integrate it with Bootstrap's Tooltip.

// Tooltip    $('button').tooltip({    trigger: 'click',    placement: 'bottom'  });    function setTooltip(message) {    $('button').tooltip('hide')      .attr('data-original-title', message)      .tooltip('show');  }    function hideTooltip() {    setTimeout(function() {      $('button').tooltip('hide');    }, 1000);  }    // Clipboard    var clipboard = new Clipboard('button');    clipboard.on('success', function(e) {    setTooltip('Copied!');    hideTooltip();  });    clipboard.on('error', function(e) {    setTooltip('Failed!');    hideTooltip();  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.5.10/clipboard.min.js"></script>  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>  <link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>    <button class="btn btn-primary" data-clipboard-text="1">Click me</button>
like image 128
Zeno Rocha Avatar answered Sep 22 '22 21:09

Zeno Rocha


I do it like it

HTML :

<button class="test" data-clipboard-text="1">Button 1</button> <button class="test" data-clipboard-text="1">Button 2</button> 

JS :

$('.test').tooltip({   trigger: 'click',   placement: 'bottom' });  function setTooltip(btn, message) {   btn.tooltip('hide')     .attr('data-original-title', message)     .tooltip('show'); }  function hideTooltip(btn) {   setTimeout(function() {     btn.tooltip('hide');   }, 1000); }  var clipboard = new Clipboard('.test');  clipboard.on('success', function(e) {     var btn = $(e.trigger);   setTooltip(btn, 'Copied');   hideTooltip(btn); }); 

With jsfiddle link https://jsfiddle.net/hs48sego/1/

like image 34
binard Avatar answered Sep 23 '22 21:09

binard