Javascript does not cooperate with positioning

I'm currently developing a feature that mimics an "autofill" functionality. Essentially, when a user enters text into an input field and the system matches this text in the database, it should display the matched string in grey within the same input field (similar to Google's autofill feature).

To achieve this, I've created two input fields - one visible to the user:

HTML:

<input id="email_input" type="text">
<input id="autofill" type="text">

CSS:

#email_input{
    background-color: transparent;
    z-index: 100;
}

Next, I dynamically position the autofill input using JavaScript to align with the email_input:

function positionAutocompleteInput(){
  var top = $('#email_input').position().top;
  var left = $('#email_input').position().left;
  $('#autofill').css({'top':top});
  $('#autofill').css({'left':left});
}
positionAutoFillInput();

The actual autofill process is implemented like this:

function autofill(context){
  var input = $('#email_input').val();
  var replacement = context[0].email;
  replacement = replacement.slice(input.length);
  var display = input + replacement;
  $('#autofill').val(display)
}

I've attempted to reposition autofill on each user input event by calling positionAutoFillInput();. However, despite checking the positions of both input fields in the console, they seem to be correctly aligned.

Both input fields share the same font-size and font-family properties.

Nevertheless, the visual output seems to be a bit off:

https://i.sstatic.net/NOqQv.png

If anyone has insights or solutions to improve this implementation, please feel free to share!

Answer №1

Is it possible to position them using only CSS? By doing so, we eliminate the need for JavaScript to handle the positioning.

#autofill, #email_input {
  font-size: 20px;
}

#autofill {
  position: absolute;
  color: #CCCCCC;
}

#email_input {
  position: relative;
  z-index: 1;
  background: transparent;
}
<h1>Test</h1>
<div>
  <input id="autofill" type="text" value="1234567890">
  <input id="email_input" type="text">
</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

What could be causing my if statement to fail even though the condition is met?

I'm attempting to generate dynamic fields based on my chosen attributes. I have two array objects called addAttributes and fakeAttributes. The fakeAttributes contain the details of the selected attributes. I have a dropdown select component that displ ...

Ways to activate a disabled button based on a condition in vue.js?

I am currently facing an issue with the 'submit' button that I have disabled under van-submit-bar. My requirement is to enable it once the user chooses a table number from the dropdown options. The default selection in the drop-down menu is &a ...

What is the best way to extract several form elements and assign a new attribute to each of them?

<form name="myform"> <fieldset> <legend>Delivery Information</legend> <p>Country: <input pattern="^[A-Za-z]" type="text" name="textInput" size=&qu ...

Concealing Content within an Accordion

Looking for some guidance on setting up a mobile version of my product image with hover features. Currently using tooltips on desktop and planning to use a modified accordion on mobile, but struggling to make progress. Customized some JS to toggle an acco ...

Processing Ajax before callback has always been a crucial step in web development. This involves utilizing features like beforeSend, ajaxStart

I am currently facing an issue with the ajax call in jQuery. What I am trying to achieve is to empty certain divs, hide a container, then make an ajax request to populate the divs again and show them. However, my code seems to be hiding the content before ...

What is the best way to ensure that child components are "ready" before proceeding with React hooks?

I have a component called Page, which contains multiple instances of the Column component, each with its own user-specific markup. Within the Page component, I need to perform additional actions (such as checking for column overflow and adjusting content ...

Issue with Jquery - Variable not getting updated on resize event, causing multiple saves

After creating this jQuery script to enable/disable a fixed position div based on user scrolling behavior, I encountered an issue where the functionality breaks upon window resizing. Despite my attempts to update the variables correctly after resizing, the ...

Bring in solely the static variable from an ES6 module

I have a file called common.js which holds all the necessary variables and methods used in my App, including a nav-bar module (nav-bar.js) among others. Typically, every module in my app will import the entire common.js module, except for the login module ...

I am experiencing difficulty with my background image loading as it requires a specific pixel value for height

Having trouble loading an image as a responsive background image in my CSS. Here's the code I'm using: .cover{ height: auto; width: 100%; background-image: url(../img/cover.jpg); background-repeat:no-repeat; background-size:c ...

Encountering a vast expanse of empty white void

Attempting to create a scrollbar within my content div instead of the entire window seems to be causing a large white space in the content box, and I'm struggling to understand why. Can someone take a look and help me identify the issue? You can view ...

Passing a state object in a POST request body, only to find it arriving empty at the Express server

I'm having an issue with my React form component when making a POST request. The body of the request is showing up as {} even though I'm trying to send my State object. Within my form Component, I am passing the state information to another Reac ...

How can I align my logo on the website header separately from the menu tabs while keeping them on the same row?

Trying to vertically align a logo and headers' tabs in the same row can be quite challenging, especially when the logo is taller than the tabs. I've attempted different methods, including using padding to manually adjust the alignment, but it&apo ...

Unable to modify the model of the directive

I'm facing an issue where additional elements added to my array within a controller of a directive are not being displayed in the view. What's even more frustrating is that when I print my model, it doesn't reflect the new elements that have ...

Executing Selenium tests: utilizing the webdriver.wait function to repeatedly call a promise

Currently, I am using Selenium ChromeDriver, Node.js, and Mocha for testing purposes... I am facing a dilemma at the moment: The driver.wait function seamlessly integrates with until. I have a promise, which we'll refer to as promiseA. This pro ...

Tips for arranging links in a vertical layout on a navigation bar that is compatible with every device

I'm having trouble aligning the links vertically in the center of the navigation bar, with the logo overlapping the content. Check out the HTML code below: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

The horizontal scrollbar is not displaying

(RESOLVED) Currently, I am developing a movie app that displays film titles through an Accordion feature using Bootstrap. Specifically, I have created an Accordion that showcases movies added to a favorites list. The intention is for this favorites secti ...

What is the best way to share the recipient's address with the browser link?

I am currently working on a project using PHP Zend with SQL Server 2008, jQuery, and AJAX. My goal is to send an email containing a webpage link to a specific client and receive feedback from them. I want to ensure that only the intended recipient is abl ...

Utilizing the fetch() method to transmit GET data within the body rather than directly embedding it in the URL

I am in the process of converting this jQuery call to vanilla JavaScript using fetch() following the guidance provided by MDN (https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Supplying_request_options). $.ajax( { ...

What is the best way to extract and manipulate text content from an HTML document using Python?

I am facing an issue where I want to extract all the text content from an HTML page that is saved locally on my computer. Currently, I have been successful in extracting all the information present on the page, but it's also including HTML tags and Ja ...

Managing the order of jQuery AJAX requests through grouping

Utilizing jQuery for triggering multiple AJAX requests, I aim to organize them in groups so that Batch 1 requests are fully executed before Batch 2 starts. The demonstration of this concept can be found on http://jsfiddle.net/ax5mty3q/. Here is the relevan ...