Ways to present a pop-up dialog box featuring word corrections

I have developed a word correction extension that encloses the incorrect word in a span element. Upon hovering over the word, a drop-down menu with possible corrections should be displayed. However, my current code is not functioning properly. How can I ensure that each correction appears aligned in a row rather than next to each other?

Is there a more efficient way to achieve this functionality?

$(".error").each(function(index, element) {
      $(this).css('cursor', 'pointer');
      $(this).mouseover(function() {
        if ($(this).has('.popup-base').length > 0) {
          return;
        }
        var popup = document.createElement('div');
        popup.className = 'popup-base';
        let correctionslist = element.getAttribute('suggestions').split(',');
        for (correct of correctionslist) {
          popup.innerHTML += '<span class="listitem">' + correct + ' </span>';
        }

        $(this).append(popup);

      });
     });
.error {
  background-color: yellow;
  position: relative;
  display: inline-block;
}

.popup-base {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  padding: 12px 16px;
  z-index: 1;
}

.error:hover .popup-base {
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div contenteditable><span data-offset-key="6j93r-0-0"><span data-text="true">Please read the <span class="error" suggestions="rules,rule,rules,reges,regrãs,regres,regras,regrá,regro,regões,rego" style="cursor: pointer;">regs</span>. My name is <span class="error"
    suggestions="nom." style="cursor: pointer;">nam</span>e <span class="error" suggestions="Nicolas,Nicola,Ricolas,Picolas,Ni colas,Nicol as,Nicolaus,Unicolas,Nicolau" style="cursor: pointer;">Nicolas</span>. Brazil is the <span class="error"
    suggestions="unto,quento,quinto,quanto,suont,qunto,duento,vento,bento,torto,mundo" style="cursor: pointer;">best</span> country in the world.</span>
  </span>
</div>

Answer №1

Your listitem elements are currently utilizing <span>, which is an inline element, causing the words to display next to each other instead of on top of each other. To resolve this issue, you can either use a different HTML element or add a class to the listitem elements with a CSS property of display:block. In the solution provided below, I have included CSS styling for the listitem class and added a hover effect for extra interactivity.

$(".error").each(function(index, element) {
      $(this).css('cursor', 'pointer');
      $(this).mouseover(function() {
        if ($(this).has('.popup-base').length > 0) {
          return;
        }
        var popup = document.createElement('div');
        popup.className = 'popup-base';
        let correctionslist = element.getAttribute('suggestions').split(',');
        for (correct of correctionslist) {
          popup.innerHTML += '<span class="listitem">' + correct + ' </span>';
        }

        $(this).append(popup);

      });
     });
.error {
  background-color: yellow;
  position: relative;
  display: inline-block;
}

.popup-base {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  padding: 12px 16px;
  z-index: 1;
}

.error:hover .popup-base {
  display: block;
}

.listitem {
  display:block;
  margin:7px 0;
}

.listitem:hover {
  background-color:blue;
  color:white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div contenteditable><span data-offset-key="6j93r-0-0"><span data-text="true">Please read the <span class="error" suggestions="rules,regulation,rules,league,reels,rider,rage,resume,race,rise" style="cursor: pointer;">regrs</span>. My <span class="error"
    suggestions="name." style="cursor: pointer;">nam</span> is <span class="error" suggestions="Nicholas,Nickolai,Nicky,Nicko,Nicolay,Nicolls,Nicholes,Nicolette" style="cursor: pointer;">Nicolas</span>. Brazil is the <span class="error"
    suggestions="point,joint,query,junta,giant,mount,pint,hunt" style="cursor: pointer;">qunto</span> <span class="error" suggestions="major,macro,meat,moor,mars,mart,mare,more,mair" style="cursor: pointer;">mear</span> country in the world.</span>
  </span>
</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

Eliminating bottom section in HTML/CSS

I've got this code snippet: new WOW().init(); /* AUTHOR LINK */ $('.about-me-img img, .authorWindowWrapper').hover(function() { $('.authorWindowWrapper').stop().fadeIn('fast').find('p').addClass('tr ...

Crafting numerous CSS templates

I am currently working on creating multiple boxes or templates (5 on each row) from a movie API. I have successfully retrieved all the necessary data and do not require a responsive design. The issue arises when I attempt to apply my CSS class to my secti ...

What could be causing the issue with the .toLocaleTimeString method not properly converting time zones?

I have been attempting to convert timezones based on the current time, but I haven't had success. I tried switching from using date.toLocaleTimeString to date.toLocaleString, but it didn't work. I also tried changing the timezone from America/Den ...

Arranging the placement of an arrow within an HTML dropdown menu

I'm struggling with positioning my custom arrow in select elements. Each select has a different width, and I need to ensure that the distance from the right edge of the select to the arrow is consistent across all of them. Currently, I am using backg ...

Is it possible for a user to modify the JavaScript code on a website?

As I develop a website that heavily relies on JavaScript, I am curious about the possibility of users being able to edit the JS code themselves. For instance, if I have an ajax function that calls a.php, could a user modify the function using Firebug or a ...

Could you please ensure that the animation activates upon hovering over the "a" element?

Utilizing bootstrap, I have created the following code. I am looking to add functionality that triggers an animation upon mouseover of an img or a element, and stops the animation upon mouseleave. .progress-bar { animation: pr 2s infinite; } @keyfr ...

Why is the responseText of an AJAX request empty on Chrome?

Why does this simple AJAX request work in IE but not Chrome? Check out the code below: var x = new XMLHttpRequest(); x.open("GET","style.php",true); x.send(); alert(x.responseText); The last line triggers an empty 'alert' window. Here's ...

What is the best way to limit the length of text in a div if it surpasses a

As I work on a website, users have the ability to add headings to different sections of a page. For example: M11-001 - loss of container and goods from Manchester Some headings can be quite detailed, but in reality, only the first few words are needed to ...

The attribute 'nameFormControl' is not a valid property on the 'ContactComponent' component

Greetings, StackOverflow community! I'm currently working on an Angular + Node app and I've run into a major problem related to forms and validators in Material Angular. Right now, I'm facing an issue where the HTML component is not recogn ...

Alter the color scheme using jQuery

Exploring the world of websites, I've come across many with a unique color switch feature. Users have the ability to choose a color, and the entire page transforms to match their selection. Check out these links for some examples... http://csmthe ...

Stop ngOnChanges from being triggered after dispatching event (Angular 2+)

In Angular 2+, a custom two-way binding technique can be achieved by utilizing @Input and @Output parameters. For instance, if there is a need for a child component to communicate with an external plugin, the following approach can be taken: export class ...

Implement consistent styling across several elements within a component

const GreenRow = styled.div` background-color: green; ` const RedRow = styled.div` background-color: red; ` const YellowRow = styled.div` background-color: yellow; ` const GreyRow = styled.div` background-color: grey; ` const MyComponent = () => ...

Adjust the alignment of two headers with varying sizes

Can someone provide guidance on aligning two headers of different sizes (e.g. h3 and h5) based on their bottom edges, making them appear like a cohesive group in a sentence format? The current code snippet I am working with is as follows: ...

Attempting to send a GET request from localhost:8080 to localhost:3000 is proving to be a challenge as I keep encountering a CORS error. Even after trying to install CORS on my node.js server, the issue

While attempting to send an axios GET request from my front-end app on localhost:8080 to my Node.js/Express.js server on localhost:3000, I encountered a CORS error. Despite installing the cors npm package and using it as middleware in my Node.js/Express.js ...

Steps to align the table to the left with the header

I created a webpage that includes a header and a table at this link Can anyone help me align the left border of the table with the left border of the header image? I'm not sure how to do it. Appreciate any assistance! ...

A guide on sorting an array based on elements from a different array

We are currently in the process of developing an application using Vue and Vuex. Our goal is to display a list of titles for venues that a user is following, based on an array of venue IDs. For instance: venues: [ {venue:1, title: Shoreline} {venue:2, ti ...

Receiving a 200 response code, however the controller's store() method is not being accessed

Recently, I made the decision to switch from using a form in a blade file to a Vue-based form that utilizes Axios for posting data. After making these changes, I encountered an issue where I receive a 200 response, but my controller's store() method i ...

The click event fails to trigger on dynamically loaded Ajax content

I've encountered an issue while loading content using ajax from external HTML Files. After the content is loaded, the click event doesn't seem to be working in Safari (including mobile safari) for any of the newly loaded elements such as ul, li, ...

Tips for leveraging a button to trigger server-side actions

Being a novice in web development, I'm currently working on a straightforward website that enables users to download files from the server side. These files are not pre-created; instead, there will be a button on the HTML page. When a user clicks this ...

Implementing CSS styling within a JavaScript file

I have a vague memory of an easy way to incorporate CSS code within a JS file, but I can't recall the specific details. Simply inserting CSS code into a JS file doesn't seem to work, so there may be a need for comments or some other method. *No ...