The element is not positioned correctly due to the absolute positioning

This is my first attempt at creating a jQuery plugin and I have almost got it working correctly. You can view the example here. I've been struggling for days to position an element as desired.

The goal is to display some text with an optional downward arrow to the right of the text. When clicked, the text/arrow should be replaced by a list of options where the currently selected option is highlighted in the same location as the original text. Clicking on an option selects it and triggers a user-defined callback. Clicking anywhere else should close the dialog.

The plugin replaces the original select element, and I want to maintain whether it is displayed inline or block. But when set to inline, the position:absolute for the <ul class="selectIt-list"> popup does not appear above the associated text in the

<span class="selectIt-text">
. Both the span text and ul popup are within a position:relative <div class="selectIt">, so why is it positioned all the way to the left?

One of the main reasons I'm doing this is to learn a consistent pattern for building plugins. My approach is heavily based on the guidelines provided at http://docs.jquery.com/Plugins/Authoring. I'm hoping someone can review my work, confirm if I'm following the correct plugin-building process, and offer any suggestions. Also, I'm unsure about namespaces. Should I post this as a separate question? Is it appropriate to ask for code critiques?

Thank you!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
        <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" /> 
        <title>Testing</title>  
        <script src="http://code.jquery.com/jquery-latest.js"></script> 
        <style type="text/css">
            /* CSS rules related to the plugin */

            div.selectIt {
                position:relative;
            }

            /* Other CSS styles... */
        </style> 

        <script> 
            // JavaScript code for the jQuery plugin...
        </script>

    </head>

    <body>
        <!-- HTML content for horizontal and vertical lists -->
    </body> 
</html> 

Answer №1

Here's a line of code from your JQuery script [line 173]:

list.css({top: top});

Include the following code right below it:

list.css({left: $(this).position().left});

Answer №2

If you're looking to position what you described, I highly recommend utilizing jQuery's "my at of" positioning method. It can make your life a whole lot easier!

Check out more information here: http://docs.jquery.com/UI/API/1.8/Position

Applying this to your specific example would involve something like the following code snippet:

//ADD THIS TO YOUR EVENT HANDLER
$(".selectIt-list").position({
    my: "bottom center",
    at: "top center",
    of: ".selectIt-text",
});

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

Equal spacing title in horizontal menu with Bootstrap design

Is there a way to evenly distribute menu links with varying text lengths in equal width bootstrap columns? Adjusting the column class between col-md-1 and col-md-3 doesn't seem to give accurate spacing. If you want to see, I've set up a fiddle h ...

When utilizing NavLink in React Router Dom for routing to an external link, a local host is automatically included in the prefix

I'm currently experiencing an issue with routing to an external link using React Router Dom 6.4. It seems that the localhost path is being appended to the URL. Can anyone provide insight on why this might be happening? localhost:3000/#/http://www.sav ...

The number input is not compatible with JavaScript/jQuery validation

While working on input field validation using javascript/jQuery code, I encountered an issue where the code worked fine with input type text but did not support input type number. Specifically, the 'number' type input did not work at all in FireF ...

Testing for ajax failure using Q-Unit in a unit test

While utilizing jQuery's $.when method to manage ajax callbacks, I am also implementing mockjax for simulating various responses in my unit tests. One of these responses results in a status error of 500. Unfortunately, when catching this error using Q ...

Tips for adjusting div content to fit a fixed height on all devices

Looking to adjust the size of the #left-content div based on the height of the window, while ensuring that all content is visible within the fixed #left-bg. However, when viewing on mobile or tablet devices, the content appears hidden. .left-bg{ backgro ...

Tips for arranging a column using a personalized display function in Handsontable

Handsontable version 0.34.2 (current version, unable to upgrade) I have a homepage displaying customer details using Handsontable. The first column is the ID column, which is a clickable HTML link. When clicked, it shows the customer's detailed infor ...

Creating a navigation bar in React using react-scroll

Is anyone able to assist me with resolving the issue I am facing similar to what was discussed in React bootstrap navbar collapse not working? The problem is that although everything seems to be functioning properly, the navbar does not collapse when cli ...

Changing a button's value on click using PhoneGap

I've been working with phonegap and came across an issue with my buttons setup like this: <input id="my_button" type="button" onclick="my_function();"/> The goal is to capture the click event and change the button's value, my_function ( ...

Top button disappears in Chromium browser after fade-in effect

I've encountered a very peculiar issue. Whenever I use the page down or fragmented identifier to jump to a specific div, my "go to top" image disappears. Here is the code snippet: HTML: <a title="Go to top" href="#" class="back-to-top"></ ...

Is there a way to trigger an interact.js event that will reset all draggables back to their original position at coordinates (0, 0)?

I am trying to figure out how to trigger an onmove or drag move event to reset the position of a draggable div to x:0 y: 0. Despite looking at various sources like help topics here and on interact.js main page, I haven't found the right solution yet. ...

Ways to implement border spacing using CSS

Here's a snippet of the code I've been working on: HTML: <html> <body> <div id="article"> <h1>TITLE</h1> <p>text</p> </div> </body> </html> CSS: #article { colo ...

Adding Firebase Authentication to my AngularJS website

I am currently creating a school website using AngularJS. My goal is to incorporate account functionality through firebase authentication. However, I have limited experience with Javascript and find working with AngularJS and implementing firebase to be ...

What situations call for the use of 'import * as' in TypeScript?

Attempting to construct a cognitive framework for understanding the functionality of import * as Blah. Take, for instance: import * as StackTrace from 'stacktrace-js'; How does this operation function and in what scenarios should we utilize imp ...

JavaScript Library function in Angular Component throwing Type Error: Function is Not Recognized

I created a custom Javascript library to group together essential functions that many Angular Components require. The library called calcs.js includes functions like this: function calculateCosts(object) { do some stuff..... return answer; } To use t ...

When pressed, the button changes color to a vibrant shade of blue, and not just a simple outline

I'm experiencing an issue with a button that turns blue when the user holds onto it for a while on mobile. You can see an example in the image below: Adding ::selected or outline did not resolve the problem as the entire button still turns blue for a ...

Disabling the submit button on an MC form - step by step guide

In order to create a multiple-choice question with radio buttons, each question must have only one answer choice. Each question should provide three options for the user to select from. It is necessary to validate whether the user has answered every questi ...

Revamping the functionality of autocomplete search

Presently, my code includes: $("#location").autocomplete({source: cities, minLength: 0, autoFocus: true}); The issue with this plugin is that it searches for matches anywhere within the input text. For instance, when I type "Bos", it not only suggests w ...

What is the best way to save a large quantity of objects to a server using ajax and php?

Imagine a scenario where I have a page containing 100 objects, each around 700 bytes when converted to json format. When it comes to saving these objects to the php based controller, there are several options available to me. Option 1: For each object ( ...

Maintaining the active state in Bootstrap, even when manually entering a URL, is essential for smooth

Check out this fully functional plnkr example: http://plnkr.co/edit/p45udWaLov388ZB23DEA?p=preview This example includes a navigation with 2 links (routing to 2 ui-router states), and a jQuery method that ensures the active class remains on the active lin ...

Styling a Fixed Header in CSS/HTML - Scroll Effect

Is it possible to hide only the upper part of the nav-bar, similar to the behavior in WhatsApp? I am using material-ui for this particular use-case. Currently, my implementation causes the app-bar to extend only when the scroll position is less than 48px, ...