Troubleshooting display issues with Chrome and Opera due to jQuery CSS interactions

I am looking to implement a feature where a floating text message appears when the user clicks on certain HTML elements. Here is the code snippet:

function showAlert(el, e){   
    var y = e.pageY;
    var x = e.pageX;
    savedState = classes[classes.length - 2]; 
    message = $("#popup");
    $(message).css('display', 'block');
    $(message).css('top', y-80).css('left', x);
    $( "<p>\""+el.textContent+"\"</p>").appendTo(message);
}

This functionality is attached to elements like this:

$(span).on("click tap", function(event){
      var element = this;
      showAlert(element, event);
 }).mouseleave(function(){
      $("#popup").css('display', 'none');
      $("#popup").children().remove();
 });

The element to be clicked is represented by 'span' and the popup message has the ID "#popup". It should appear with the following CSS properties:

var popupElement= $("<div id='popup'></div>");
var styles = {
            "display" : "none",
            "position":"absolute",
            "background-color":"#fff",
            "border":"1px solid #000",
            "padding":"5px",
            "max-width":"200px",
            "color":"black",
            "font-size":"10px",
        }
        $(popupElement).css( styles );
        $(popupElement).appendTo($("body"))

While this code works well in most browsers, there are issues in Chrome and Opera where the message does not show up. If I remove the 'display: none' property on mouse leave, a small white square appears but with no text inside. This might be related to the 'display' property manipulation, but I'm unsure. Is there a potential compatibility problem that I have overlooked?

Answer №1

It seems that the issue stems from clicking on the span element causing the message to display below the mouse pointer, inadvertently triggering the mouseleave event and hiding the message immediately. To troubleshoot this problem, try disabling the mouseleave event callback and observe the behavior. Additionally, experiment with adjusting the top/left values in the following code snippet:

$(msg).css('top', y-80).css('left', x);

Make modifications to the x/y values to ensure that your message div does not appear directly under the cursor.

Answer №2

Highlighted by Anna Smith in the discussion, it was pointed out that using the `new` keyword to name a variable will trigger a syntax error.

It seems this issue persists across various browsers and JS engines. Consider renaming the variable to something like `_new` instead.


Latest Update

After adjusting the variable name and removing a seemingly irrelevant line as per recommendation:

saved_state = classi[classi.length - 2];

The code now functions flawlessly. You can view the updated version on jsfiddle: https://jsfiddle.net/jormaechea/4b7r66vr/

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

Designing a hierarchical HTML dropdown menu for choosing an XML file for parsing using a jQuery script

I have successfully created a question and answer framework using Jquery and XML files. Users can choose a specific category of questions from a drop-down menu, which then displays the corresponding questions and answers from the selected XML file. While ...

Javascript code for scrolling to the top isn't functioning as expected

I found a helpful code snippet for adding a scroll to top button on my webpage at the following link: http://jsfiddle.net/neeklamy/RpPEe/ Although I implemented the code provided, unfortunately, the scroll to top button is not displaying correctly. Here ...

Bootstrap grid with Flexbox to create a visually appealing layout with images of varying sizes

I am facing a challenge with images of different dimensions that I need to display at the same height within a Bootstrap grid. Despite trying various methods like using "flex 1" on child items and "flex: 1 0 0%;" on the source, I have not been successful ...

Having issues with animation performance in IE11

I've encountered an issue with an animation that is functioning correctly on all browsers except for IE11. $(document).ready(function() { $('.activate').click(function() { $('.eight-box').toggleClass('animate') ...

Leveraging the On Keyup Function in Real-Time

How can I modify this code to run after an ajax call using .live method? var element=document.getElementById('txt_url'); element.onkeyup=function(){ var input=element.value; if(input=='') return; if(input.indexOf('h ...

Retrieve the content of the nearest 'td' element using the '.closest()' method, then locate the desired

I am struggling to assign the value from a <td> to a variable. My approach involves utilizing the closest() and find() methods in jQuery to locate the desired <td>. Interestingly, when I use alert on the <td>, it displays the correct val ...

HTML: Efficiently updating multiple cell contents in a large table using jQuery or JavaScript

Hello, I am currently working on developing an HTML page that consists of a large data table. My goal is to have the data in the table update dynamically as the user interacts with various controls on the page, without having to reload the entire page. ...

Unable to post form attribute value after submission

I have created a form that looks like this: <form method="POST" action="create.php" data-id="0" class="postForm"> <input type="hidden" id="#formId" value="1"> <textarea class="formBodyText"></textarea> <button typ ...

Exploring the JSON object tree through recursion using underscore and backbone

If I have a JSON object with nested arrays of unknown depths and I want to pass each array into a _.template function, how can I make that happen? Here's an example of what my JSON object might look like: $start_elements = array ( array( ...

Display a single button on hover in Angular 2+ framework

I'm looking to have the 'Edit' button only appear when I hover over a selected row. Here's the code: https://stackblitz.com/edit/inline-edit-change-edit2-j8b2jb?file=app%2Fapp.component.html I want the 'Edit' button to show u ...

Why does my Jquery Function only work on my Local Server and not on the Live Server?

I'm encountering a small issue with my horizontal drop-down menu bar. I've implemented jQuery functions to toggle Parent and child items, which are working smoothly on my local server but not on the live server. I have double-checked all script f ...

Can a collection of HTML elements be grouped together for synchronized movement?

When the browser size is adjusted or viewed on different devices, it is necessary for a group of related HTML elements to stay together and move as a block. This means that if one element moves to the next "row" due to lack of space, all elements should sh ...

Using JavaScript, the list of items (images) will be displayed and placed into HTML panels

Below is the layout structure on my website: <div class="panel-heading"><h3 class="panel-title">Suggestions</h3></div> <div class="panel-body"> <div class="col-md-7"> <h3><span class= ...

Exploring methods for comparing AJAX and PHP values within an AJAX success function

const salesId = $('#assign_sales_id').val(); $('#'+id).children('td[data-target=sales_id]').text(salesId); I am interested in verifying if the selected name from users matches the ID: id = '$('#assign_sales_id' ...

CSS auto width not applied to images causing a lack of scaling

I recently added this code to my webpage: image { margin: 0 2px 0 0; height: 100px; width: auto !important; overflow: hidden; text-align: center; } The goal was to scale all images to a maximum height of 100px and have the browser adj ...

Jquery Autocomplete Feedback

I have a form where I am using Jquery autocomplete. Once the user selects a value from the autocomplete suggestions, I need to retrieve that value and pass it to another jquery function. Based on the selected value, I also want to trigger another autosugge ...

The Laravel controller is producing a JSON response that is coming back as undefined

Greetings! I am working with a JSON array and running the following code in my AJAX call: $('tbody').html(data.table_data); When I receive the response, it looks like this: [{"id":28,"fname":"tester","lname":"testlast","phone":"00000000","emai ...

What is the best way to make my text stand out against a faded background?

After mastering the basics of web development on Codecademy, I was eager to start my own blog and create a unique website. One challenge I encountered was figuring out how to fade the background without affecting the text. Despite researching various solut ...

Is there a way to customize the color of the HR element in a Material-UI Select Field?

https://i.stack.imgur.com/DYeX7.png https://i.stack.imgur.com/CN0T6.png Hi there, I am currently working on a website and using a Select Field component from Material-UI. I am faced with the challenge of customizing the style to change the default light ...

Finding distinct values in a multidimensional array using JavaScript

When working with JavaScript, I created a multidimensional array using the following code: result.each(function(i, element){ init.push({ label : $(this).data('label'), value : $(this).val(), }); }); The resulting array in ...