I am trying to implement a small popup box that appears when I hover over some text, but for some reason it's not working as expected. Can someone please help me troubleshoot this issue? My goal is to display the content from the "data-popup" attribute when hovering over the word "surpi". The HTML line containing this dynamic content is generated using JavaScript. Here is the code snippet:
<a href="#" data-popup="surpris">supri</a>
$(document).ready(function() {
popup();
});
function popup() {
$("#correction").on("mouseover", "a", function() {
var data = $(this).attr("data-popup"),
offsetDataTop = $(this).offset().top,
offsetDataLeft = $(this).offset().left;
if (data != "") {
// Create .popup element
$("body").prepend("<div class='popup'>" + data + "</div>");
// Set properties for .popup
var popupWidth = $(".popup").innerWidth(),
thisWith = $(this).innerWidth(),
marginLeft = (thisWith - popupWidth) / 2;
// Initialize .popup
$(".popup").css({
opacity: 0,
top: offsetDataTop - 40,
left: offsetDataLeft + marginLeft
});
// Animate .popup
$(".popup").animate({
opacity: 1,
marginTop: 20
}, "fast");
}
}, function() {
$(".popup").remove();
}); // Remove .popup
};