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

Showing an image on a blurred background-image at the top of the webpage

Is it possible to overlay an image on top of another image, with the top image appearing blurred in the background? .background-image { background-image: url('https://images.pexels.com/photos/366968/pexels-photo-366968.jpeg?w=1153&h=750&a ...

In Flow, how is the asterisk (*) type utilized and what is its counterpart in TypeScript?

My expertise lies mostly in TypeScript, but I recently came across Flow which seems quite similar to TS in many aspects. However, one thing that caught my attention is the asterisk (*) type in Flow. Initially, I thought it was just another way of represent ...

In Chrome, CSS automatic hyphens are displayed as question mark symbols

On my website, I have implemented automatic hyphenation using CSS: article > p, article > li { -o-hyphens: auto; -o-hyphenate-limit-before: 3; -o-hyphenate-limit-after: 3; -o-hyphenate-limit-chars: 6 3 3; -o-hyphenate-limit-lines: 2; -o-h ...

Incorporate external JavaScript libraries not built with Angular

Our project utilizes NPM in conjunction with Browserify for managing third-party dependencies, which pairs well with AngularJS due to the CommonJS-modules. Below is a code snippet showcasing the dependency structure that integrates seamlessly with Angular ...

Enhance the HTML content using a JavaScript function

Here is the code that I have: <label>Brand</label></br> <select name="brand" id="brand" onChange="changecat(this.value);"> <option value="" selected>Select Brand</option> <option value="A">AMD</option&g ...

Is it possible to capture a submit event from a form within an iframe using jQuery or JavaScript

If I have a webpage with an embedded iframe containing a form, how can I update a hidden field value on the main page once the form is submitted? What is the best way to trigger an event in the parent page upon form submission? Here's a simplified ex ...

Make sure link opens in the same window/tab

Currently, I am utilizing the Open Link in Same Tab extension on Google Chrome. This extension can be found here. The purpose of this extension is to ensure that all links open in the same window/tab, which is necessary for touch screen kiosk left/right s ...

Methods for assigning values to a formControl using an array

I have an array of objects and I am attempting to loop through the array, dynamically setting values to a formControl and not displaying anything if the value is null. I have searched for similar solutions but haven't found any references or examples ...

What is the best way to ensure that cleanup is always executed at the conclusion of a function

Imagine a scenario where I have the following function (psuedo-code) : function Foo() { let varThatNeedsCleanup = //something if(condition1) { return Error1; } if(condition2) { return Error2; } if(condition3) { return Error3; ...

Mongoose Error: The function 'mongooseSchemahere' is not recognized as a valid function

Here is the mongoose Schema setup in models/user.js: const mongoose = require('mongoose'); const userSchema = mongoose.Schema({ loginId: String, firstname: String, lastname: String, eMail: String, password: String, acti ...

Is the "sizes" attribute of the "picture" element functional in Chrome when it comes to <source>?

The incredible <picture> element has been functioning flawlessly in the Chrome browser for well over a year, yet regrettably, I seem to be encountering an obstacle when attempting to make use of the powerful sizes attribute within the <source> ...

Issue with React Router version 6: displaying an empty page

I am currently grappling with implementing react-router for my react applications. However, I am encountering issues with the routing part as it consistently displays a blank page. Below are snippets of the code from the involved files: index.js import R ...

Failure of AJAX to transmit variable to PHP script

I am relatively new to PHP and currently working on the dashboard page of a website where an administrator can view all existing admins. Each admin's row has a button that is supposed to check their privileges, such as access to article editing and cl ...

Understanding the reading of Java Class List in Vue Js

As a beginner in Front-end development, I am trying to figure out how I can use Vue.js to read a Java class List. Specifically, I have a Java class that includes a list of fruits and I want to be able to display this list on the UI using Vue.js: public c ...

The ajax request functions smoothly on the OS X Paw app and cURL, but does not work properly when coded in Javascript / jQuery

Currently delving into the world of ajax requests, I've been utilizing the Paw app to troubleshoot one. Surprisingly, the request functions flawlessly within Paw itself and even the cURL code generated by Paw works like a charm. However, the JavaScrip ...

Attempting to include a standard VAT rate of 5% on the invoice

/* The code format is correct and I don't see any issues on my end, I hope it works well for you. */ I need assistance in adding a fixed VAT (tax) rate of 5%. The tax amount should be displayed on the row, while the total tax should reflect the sum o ...

Retrieve container for storing documents in JavaServer Pages

Previously, I created JSP and HTML code to upload a file from the hard disk into a database using <input type="file" name="upfile"> However, a dialog box with an "Open" button is displayed. What I am looking for is a "Save" button that will allow u ...

What is the best way to design a dynamic menu using HTML, CSS, and jQuery, where each li element gradually disappears?

Consider this menu structure: <ul class="main-menu"> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> </ul> My task is to dynamically hide th ...

The NestJS HttpException class will default to a status code of 201 if no specific status code is

This particular instance showcases how instantiating the HttpException class in this manner results in an exception being thrown with an undefined status, ultimately becoming a status of 201 (I presume it defaults to this status as it is associated with a ...

Utilize an object model property as an array key for filtering in the AngularJS and MEANJS framework

I am currently working on a MEANJS application that includes a CRUD module for countries. When I select a country from the default list view, it redirects me to the view-country.client.view page. In order to enhance my model, I have introduced a field name ...