Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The tooltip is not visible when I click the icon. How can I fix this code so that it is visible?

I have made an abbreviations list so that when a user clicks the clipboard icon, the abbreviation definition will be copied to the clipboard. I added a tooltip that appears on clicking the icon saying ‘Copied’ to alert the user. However, I cannot figure out why the tooltip is not visible when I click the icon. Here is my code:

$(document).ready(function () {
  /* COPY TO CLIPBOARD */

  // click the icon
  $(".copy-icon").on("click", function (e) {
    e.preventDefault();

    // Find the parent list item
    let listItem = $(this).closest("li");

    // Find the abbreviation and definition within the list item
    let abbreviation = listItem.find("p").text();

    // Create a temporary input element
    let $temp = $("<input>");
    $("body").append($temp);
    $temp.val(abbreviation).select();
    document.execCommand("copy");
    $temp.remove();

    // Show the tooltip
    let tooltip = $("<span class='tooltip'>Copied</span>");
    listItem.append(tooltip);

    // Remove the tooltip after 1 second
    setTimeout(function () {
      tooltip.remove();
    }, 1000);
  });
});
*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: inherit;
}

html {
  box-sizing: border-box;
  overflow-y: scroll;
}

body {
  font-family: "Poppins", sans-serif;
  background-color: #f0f0f0; /* Background color for the body */
}

/* Centering content */
.center {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Container for the abbreviations list */
.container {
  display: grid;
  grid-template-columns: repeat(2, 1fr); /* Two columns for two rows */
  gap: 20px; /* Adjust the gap as needed */
  justify-content: center; /* Center the columns horizontally */
  font-family: "Poppins", sans-serif;
  padding: 0 20px; /* Add padding to the sides */
}

/* Abbreviations list row */
.abbreviations-row {
  list-style: none;
  padding: 0;
}

/* Abbreviation list item */
.abbreviations-list li {
  display: flex;
  align-items: center;
  background-color: #fff; /* Background color for list items */
  border: 1px solid #ccc; /* Border for list items */
  border-radius: 8px; /* Rounded corners for list items */
  margin: 10px;
  padding: 10px;
  position: relative;
}

/* Copy icon */
.copy-icon {
  color: #2a7475;
  font-size: 24px;
  margin-right: 10px; /* Spacing between icon and text */
  cursor: pointer;
}

.copy-icon:hover {
  transform: translateY(-4px);
  opacity: 1;
}

.abbreviations-list li {
  position: relative; /* Add this line to set a non-static position */
  /* ... Other styles ... */
}

.tooltip {
  position: absolute;
  top: -30%;
  left: 50%;
  transform: translateX(-50%);
  background: #373737;
  padding: 10px 15px;
  color: #fff;
  font-size: 14px;
  border-radius: 4px;
  letter-spacing: 1px;
  opacity: 0;
  z-index: 1; /* Ensure tooltip appears above other elements */
  transition: opacity 0.3s ease-in-out;
}

.tooltip.appear {
  animation: appear 1s ease;
}

@keyframes appear {
  0% {
    opacity: 0;
  }
  20% {
    transform: translateY(10px);
    opacity: 1;
  }
  80% {
    transform: translateY(0px);
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Abbreviations List</title>
    <link
      href="https://fonts.googleapis.com/icon?family=Material+Icons"
      rel="stylesheet"
    />
    <link
      href="https://fonts.googleapis.com/css?family=Poppins&display=swap"
      rel="stylesheet"
    />
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <div class="header">
      <h3>Abbreviations List</h3>
    </div>
    <div class="center">
      <div class="container">
        <div class="abbreviations-row">
          <ul class="abbreviations-list">
            <li>
              <span class="tooltip">Copied</span>
              <i class="material-icons copy-icon">content_copy</i>
              <p>ACR, American College of Rheumatology criteria</p>
            </li>
            <li>
              <span class="tooltip">Copied</span>
              <i class="material-icons copy-icon">content_copy</i>
              <p>AE, adverse event</p>
            </li>
            <li>
              <span class="tooltip">Copied</span>
              <i class="material-icons copy-icon">content_copy</i>
              <p>AS, ankylosing spondylitis</p>
            </li>
            <!-- Add more abbreviations as needed -->
          </ul>
        </div>
        <div class="abbreviations-row">
          <ul class="abbreviations-list">
            <li>
              <span class="tooltip">Copied</span>
              <i class="material-icons copy-icon">content_copy</i>
              <p>
                ACR 20/50/70, ≥20/≥50/≥70% response in the American College of
                Rheumatology criteria
              </p>
            </li>
            <li>
              <span class="tooltip">Copied</span>
              <i class="material-icons copy-icon">content_copy</i>
              <p>API, abbreviated prescribing information</p>
            </li>
            <li>
              <span class="tooltip">Copied</span>
              <i class="material-icons copy-icon">content_copy</i>
              <p>ASAS, Assessment of SpondyloArthritis international Society</p>
            </li>
            <!-- Add more abbreviations as needed -->
          </ul>
        </div>
      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  </body>
</html>

I have asked chatgpt what is wrong with the code and it says nothing is wrong and the tooltip should be visible

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have a tooltip class with the opacity of 0 and you override it by adding the appear class. However, when you append your dynamic tooltip, you never add the appear class.

Instead of fixing that, I simply removed that code and reused the tooltip span that already exists in your HTML. There was no reason to recreate it. I simply used listitem to find the child of tooltip and added the appear class. I removed the timeout since your appear and tooltip has a transition that changes the opacity after a second anyway.

$(document).ready(function () {
  /* COPY TO CLIPBOARD */

  // click the icon
  $(".copy-icon").on("click", function (e) {
    e.preventDefault();

    // Find the parent list item
    let listItem = $(this).closest("li");

    // Find the abbreviation and definition within the list item
    let abbreviation = listItem.find("p").text();

    // Create a temporary input element
    let $temp = $("<input>");
    $("body").append($temp);
    $temp.val(abbreviation).select();
    document.execCommand("copy");
    $temp.remove();

    // Show the tooltip
    listItem.find(".tooltip").addClass("appear")
  });
});
*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: inherit;
}

html {
  box-sizing: border-box;
  overflow-y: scroll;
}

body {
  font-family: "Poppins", sans-serif;
  background-color: #f0f0f0; /* Background color for the body */
}

/* Centering content */
.center {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Container for the abbreviations list */
.container {
  display: grid;
  grid-template-columns: repeat(2, 1fr); /* Two columns for two rows */
  gap: 20px; /* Adjust the gap as needed */
  justify-content: center; /* Center the columns horizontally */
  font-family: "Poppins", sans-serif;
  padding: 0 20px; /* Add padding to the sides */
}

/* Abbreviations list row */
.abbreviations-row {
  list-style: none;
  padding: 0;
}

/* Abbreviation list item */
.abbreviations-list li {
  display: flex;
  align-items: center;
  background-color: #fff; /* Background color for list items */
  border: 1px solid #ccc; /* Border for list items */
  border-radius: 8px; /* Rounded corners for list items */
  margin: 10px;
  padding: 10px;
  position: relative;
}

/* Copy icon */
.copy-icon {
  color: #2a7475;
  font-size: 24px;
  margin-right: 10px; /* Spacing between icon and text */
  cursor: pointer;
}

.copy-icon:hover {
  transform: translateY(-4px);
  opacity: 1;
}

.abbreviations-list li {
  position: relative; /* Add this line to set a non-static position */
  /* ... Other styles ... */
}

.tooltip {
  position: absolute;
  top: -30%;
  left: 50%;
  transform: translateX(-50%);
  background: #373737;
  padding: 10px 15px;
  color: #fff;
  font-size: 14px;
  border-radius: 4px;
  letter-spacing: 1px;
  opacity: 0;
  z-index: 1; /* Ensure tooltip appears above other elements */
  transition: opacity 0.3s ease-in-out;
}

.tooltip.appear {
  animation: appear 1s ease;
}

@keyframes appear {
  0% {
    opacity: 0;
  }
  20% {
    transform: translateY(10px);
    opacity: 1;
  }
  80% {
    transform: translateY(0px);
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Abbreviations List</title>
    <link
      href="https://fonts.googleapis.com/icon?family=Material+Icons"
      rel="stylesheet"
    />
    <link
      href="https://fonts.googleapis.com/css?family=Poppins&display=swap"
      rel="stylesheet"
    />
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <div class="header">
      <h3>Abbreviations List</h3>
    </div>
    <div class="center">
      <div class="container">
        <div class="abbreviations-row">
          <ul class="abbreviations-list">
            <li>
              <span class="tooltip">Copied</span>
              <i class="material-icons copy-icon">content_copy</i>
              <p>ACR, American College of Rheumatology criteria</p>
            </li>
            <li>
              <span class="tooltip">Copied</span>
              <i class="material-icons copy-icon">content_copy</i>
              <p>AE, adverse event</p>
            </li>
            <li>
              <span class="tooltip">Copied</span>
              <i class="material-icons copy-icon">content_copy</i>
              <p>AS, ankylosing spondylitis</p>
            </li>
            <!-- Add more abbreviations as needed -->
          </ul>
        </div>
        <div class="abbreviations-row">
          <ul class="abbreviations-list">
            <li>
              <span class="tooltip">Copied</span>
              <i class="material-icons copy-icon">content_copy</i>
              <p>
                ACR 20/50/70, ≥20/≥50/≥70% response in the American College of
                Rheumatology criteria
              </p>
            </li>
            <li>
              <span class="tooltip">Copied</span>
              <i class="material-icons copy-icon">content_copy</i>
              <p>API, abbreviated prescribing information</p>
            </li>
            <li>
              <span class="tooltip">Copied</span>
              <i class="material-icons copy-icon">content_copy</i>
              <p>ASAS, Assessment of SpondyloArthritis international Society</p>
            </li>
            <!-- Add more abbreviations as needed -->
          </ul>
        </div>
      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  </body>
</html>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading