Is there a way to increase the size of the icon without altering the dimensions of the button?

After implementing a code snippet to enable a button to copy the URL to the clipboard, I encountered an issue. Originally, the button displayed the text "copy," which changed to "copied" after clicking on it and reverted back after 2 seconds.

However, I decided to replace the text with an icon. The problem now is that the icon transitions into the old "copied" text and then to nothing after 2 seconds since the icon is not textual. How can I make it revert back to the icon instead? Additionally, I want to replace the "copied" text with the same icon but slightly larger. What method or function should I use to achieve this?

I managed to fix the transition back to the icon. However, I'm unsure how to increase the size of the icon using JavaScript. Is there a way to change the font-size dynamically through JavaScript?

I successfully adjusted the font size with the help of this resource. Nonetheless, I am facing another challenge where the button's size changes due to adjusting the font size.

EDIT: I was able to resolve the entire issue. Thank you for all the assistance!

var $temp = $("<input>");
var $url = $(location).attr('href');

$('.clipboard').on('click', function() {
const originalText = $('.clipboard').html();

// CSS function:
const addCSS = s => document.head.appendChild(document.createElement("style")).innerHTML=s;

$("body").append($temp);
$temp.val($url).select();
document.execCommand("copy");
$temp.remove();
$('.clipboard').html(originalText);
// CSS Usage: 
addCSS(".clipboard{ font-size: 20px; padding: 4.8px 21.3px 4.8px 21.3px; }")

// Run something in 1 second to revert back to the button's text
setTimeout(function() { 
addCSS(".clipboard{ font-size: 15px; padding: 9px 21.3px 9px 21.3px}")
$('.clipboard').html(originalText);
}, 1000); // 1 second

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="background">
  <center>
    <button class="clipboard"><i class="fas fa-copy"></i></button>
  </center>
</div>

Answer №1

One effective method is to enclose each potential button content within a span element (or another inline element) and switch between them as required. This approach eliminates the need to monitor and generate text content individually. Utilize CSS to style each inner element as necessary, simplifying the process.

var $temp = $("<input>");
var $url = $(location).attr('href');

$('.clipboard').on('click', function(e) {
  const btn = $(this);

  // copy to clipboard
  $("body").append($temp);
  $temp.val($url).select();
  document.execCommand("copy");
  $temp.remove();

  // show copied message
  btn.find('i').hide();
  btn.find('.copied-text').show();

  // Wait 1 second to revert the button content
  setTimeout(function() {
    btn.find('.copied-text').hide();
    btn.find('i').show();
  }, 1000); // 1 seconds

})
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" referrerpolicy="no-referrer"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<style>
  .background {
    text-align: center;
  }
  
  button {
    font-size: 20px;
    padding: 4.8px 21.3px 4.8px 21.3px;
    min-width: 140px;
  }
  
  .copied-text {
    display: none;
    font-size: 15px;
    padding: 9px 21.3px 9px 21.3px;
  }
</style>

<div class="background">
  <button class="clipboard">
    <i class="fas fa-copy"></i>
    <span class="copied-text">Copied</span>
  </button>

  <button class="clipboard">
    <i class="fas fa-copy"></i>
    <span class="copied-text">Copied</span>
  </button>
</div>

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

FNS Date-Timezone Abbreviation

Is there a way to shorten the Australian Eastern Daylight Time abbreviation to just AEDT? When I use it currently, it displays as 11/11/2022 15:29:25 Australian Eastern Daylight Time. I would like it to show as 11/11/2022 15:29:25 AEDT import { formatInT ...

Troubleshooting Problems with CSS Printing on iOS Devices

On desktop (both Windows and Mac), the print preview displays correctly. However, on iOS devices, the full width is not shown and there is some blank space above. The issue I'm facing is that I cannot debug this problem using Browserstack. I am restr ...

What is the best way to create a <div> that will automatically start a new line based on the user's input?

Is it possible to create a div that automatically inserts a new line for each new line in the code it detects when typing? For example, if I type: <div> Hello, World! How are you doing today? </div> This would normally be displayed as ...

switch from material ui lists on/off

Trying to learn React through coding, I've encountered an issue with displaying the 'StarBorder' icon next to folders when clicked. Currently, clicking on any folder displays the 'StarBorder' icon for all folders. Any tips on how t ...

Tips for Aligning h5 Headings with Unordered Lists

My footer contains the following code: HTML: <div class="container"> <div class="row"> <div class="col-md-6"> <div class="pull-right"> <h5><u><i>Information</i></u> ...

Using a static value in the comparator is necessary for Array.find to function properly in Typescript

Looking to retrieve an item from an array: const device = this.selectedDevtype.devices.find(item => console.log(this.deviceID); return item.device_id === this.deviceID; }); console.log(device); When this.deviceID is logged, it shows "4", but t ...

I just installed Electron on my Mac using the command 'sudo npm install electron -g', but now I am encountering an error. How can I resolve this issue

When I first attempted to run the command, I encountered 'Permission Denied' errors so I added sudo before the command as suggested. Another recommendation was to install the electron folder at /usr/local/lib/node_modules, but even after reinstal ...

Beware: The use of anonymous arrow functions in Next.js can disrupt Fast Refresh and lead to the loss of local component state

I am currently encountering a warning that is indicating an anonymous object in a configuration file, and even specifying a name for it does not resolve the warning. Below you will find the detailed warning message along with examples. Warning: Anonymous ...

Samsung browser encounters an international error

After developing my web application using Angular2 Rc1, I noticed that it functions perfectly on Safari, Firefox, and Chrome browsers. However, when trying to access the application on my Galaxy S6 using the default browser, an error pops up: https://i.s ...

Encountering an Issue: Unable to Generate Line Chart with gRaphael Library

I'm currently working with the gRaphael JavaScript library to create a basic line graph. Here is the code I have implemented on my page: <script language="javascript" type="text/javascript"> var paper = Raphael(10, 50, 640, 480); paper.g.line ...

I am interested in dynamically rendering the page on Next.js based on certain conditions

In my -app.js file, I have the code snippet below: import { useState, useEffect } from "react"; import PropTypes from "prop-types"; ... export default function MyApp(props) { const { Component, pageProps } = props; co ...

Using Node.js to import modules that have been webpacked

I'm faced with the challenge of managing a multitude of files that come along when installing various modules using npm install, each with its own dependencies. To simplify this process, I am considering consolidating all required libraries using web ...

Adding text to an existing div element using jQuery and ensuring the div tag automatically adjusts its height according to the text length

I have attempted to implement the following code, but unfortunately, it is not functioning as expected. I am hopeful that someone can assist me. I am seeking a way for the div tag to dynamically increase its height based on the size of the text. Does any ...

Issue with Angular filter not being effective within ng-repeat

For all code regarding this issue, please visit: Filter: <input type="text" ng-model="search"> <tr ng-repeat="(id, group) in groups | filter:search" class="editing-{{group.editing}}"> The goal is to have the rows filtered out based on the in ...

How can you declare variables in CSS using LESS?

I am facing a challenge where I need to dynamically change the branding color and other styling variables on the portal based on certain conditions at runtime. I have successfully implemented this functionality using CSS with the code snippet provided be ...

Troubleshooting issue: Asynchronous functionality not working with Ajax.BeginForm

Struggling to grasp ASP.Net MVC and facing challenges with using Ajax.BeginForm to update a partial view asynchronously. Here's the code snippet in the view for the action: @using (Ajax.BeginForm( new AjaxOptions { ...

Having difficulty toggling a <div> element with jQuery

I am attempting to implement a button that toggles the visibility of a div containing replies to comments. My goal is to have the ability to hide and display the replies by clicking the button. The "show all replies" button should only appear if there are ...

Enable highlighting a table border when hovering over it

Is there a way to highlight the boundary of a table row when hovering over it? Something like this: I attempted to use CSS with the following code: .ant-table-tbody>tr.ant-table-row:hover>td, .ant-table-tbody>tr>td.ant-table-cell-row-hover{ ...

What is the best method for positioning 2 buttons at the bottom of a card using Bootstrap?

I need help with adjusting the position of two buttons in my bootstrap design. I would like both buttons to be located at the bottom of the card and have a slight gap between them, perhaps around 5 pixels. Below is the snippet of code: <div class=" ...

Working with the React JS Reducer, I am limited to only printing the returned payload and unable to perform any other actions

I've been struggling with this problem non-stop for several days now. Here's my reducer file: import {ADD_TASK, GET_TASKS, GET_TASKS_ERROR, GET_TASKS_SUCCESS} from "./TaskActions"; export const INITIAL_STATE = { tasks: [], isFetching: ...