Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8776373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:02:19+00:00 2026-06-13T19:02:19+00:00

I am creating my first jQuery plugin and have a somewhat working example (see

  • 0

I am creating my first jQuery plugin and have a somewhat working example (see http://tapmeister.com/test/selector.html) and script below. I have been struggling with positioning an element for days.

The desired look and functionality starts with showing some text and an optional down arrow to the right of the text. Upon clicking, the text/arrow is replaced with a list of options with the currently selected option highlighted and at the same location of the original text. Clicking an option makes it the currently selected option, and initiates a user defined callback, and clicking any other space closes the dialog.

The plugin replaces the original select element, and I wish to preserve whether it is inline or block. When making them inline, however, the position:absolute <ul class="selectIt-list"> popup does not appear above it’s associated text <span class="selectIt-text">. Since both the span text and ul popup are both in position:relative <div class="selectIt">, why is it located to the far left?

As a side note, a major reason why I am doing this is to learn a consistent pattern on how plugins should be built. I am strongly basing my approach on http://docs.jquery.com/Plugins/Authoring (any reason I shouldn’t?). I am hoping someone can review it, confirm whether I am correctly interpreting how to build a plugin, and provide any suggestions. I am also not sure whether I really get the whole namespace thing. Maybe I should post this as a separate question?, but I don’t know whether asking for code critique is appropriate.

Thanks!

<!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 associated with plugin */

            div.selectIt {
                position:relative;
            }

            span.selectIt-text {
                color: #2C46B7;
                cursor: pointer;
                font-family: Verdana;
                font-size: 10px;
                display:inline;
            }

            span.selectIt-text,
            ul.selectIt-list {
                padding: 7px 3px 5px;
            }

            ul.selectIt-list {
                display:none;
                margin:0px;
                position: absolute;
                background-color: #FFFFFF;
                border: 1px solid #B3B4A7;
                z-index: 50;
                box-shadow: 3px 1px 6px #505050;
            }

            ul.selectIt-list li {
                background-color: #FFFFFF;
                color: #393926;
                list-style-type: none;
                font-family: Verdana;
                font-size: 10px;
                margin-bottom: 2px;
                padding-left: 2px;
                padding-right: 2px;
            }
            ul.selectIt-list li.selectIt-hover {
                background-color: #DFDED8;
                color: #393926;
            }
            ul.selectIt-list li.selectIt-selected {
                background-color: #737060;
                color: #FFFFFF;
                cursor: default;
            }

            /* CSS Not associated with plugin */

            ul.myList > li {
                list-style-type: none;
                display:inline;
            }
            #horizontalList select.mySelect {
                display:inline;
            }
            #verticalList select.mySelect {
                display:block;
            }
            span.label {
                color: rgb(57, 57, 38);
                font-family: Verdana;
                font-size: 10px;
                font-weight: bold;
            }
            span.selectIt-text {
                background-image: url("dropdown_arrow_blue.gif");
                background-position:right center; 
                background-repeat: no-repeat;
                padding-right: 10px; /*Override padding-right to allow for image*/
            }

        </style> 

        <script> 
            /*
            * jQuery SelectIT
            * Copyright 2012 Michael Reed
            * Dual licensed under the MIT and GPL licenses.
            * 
            */
            (function( $ ){

                var methods = {
                    init : function( options ) {
                        //console.log('init');

                        // Create some defaults, extending them with any options that were provided
                        var settings = $.extend({
                            'class' : null, //Applies custom class so user may override
                            'extraWidth' : 0,  //Extra space to add to width to allow for user image css
                            'click' : function(){}   //Some user defined code.
                            }, options  || {}); //Just in case user doesn't provide options

                        //Apply events that should only happen once (unlike what is done by http://docs.jquery.com/Plugins/Authoring#Events. Why did they do it that way?)
                        $(document).on('click.selectIt',methods.close);

                        return this.each(function(){

                            var $t = $(this),
                            data = $t.data('selectIt');

                            //Replace element
                            var list = $('<ul/>', {'class': "selectIt-list" }),
                            length,
                            max=0;
                            $t.find('option').each(function(i){
                                var $this=$(this),
                                text=$this.text(),
                                length = parseInt($this.css('width'));
                                max = length > max ? length : max;
                                list.append( $('<li/>', {text: text}))
                            });
                            max=max+settings.extraWidth;
                            list.css('width',max+'px');

                            var selectIt = $('<div/>', {class:"selectIt"+((settings.class)?' '+settings.class:'')}) //Didn't work to display same as original element: display:$t.css('display')
                            .append($('<span/>', {class:'selectIt-text',text:$t.find(":selected").text()}))
                            .append(list)
                            .css({display:$t.css('display')});  //Set to same as original element

                            //Apply events
                            selectIt.on('click.selectIt','span.selectIt-text',methods.open);
                            selectIt.on('mouseenter.selectIt','ul.selectIt-list li',methods.mouseenter);
                            selectIt.on('mouseleave.selectIt','ul.selectIt-list li',methods.mouseleave);
                            selectIt.on('click.selectIt','ul.selectIt-list li',methods.click);

                            // If the plugin hasn't been initialized yet.  Why?
                            if ( ! data ) {
                                // Do more setup stuff here
                                $t.data('selectIt', {
                                    target : $t,     //Not really sure where this will be used
                                    selectIt : selectIt,  //Will be used when removing in destroy method
                                    settings: settings  //Save here so other methods have access
                                });

                            }
                            $t.hide().after(selectIt);
                        });
                    },

                    destroy : function(e) {
                        //console.log('destroy');
                        return this.each(function(){
                            var $t = $(this);
                            $t.off('.selectIt'); //Removes any events in selectIt namespace
                            $t.data('selectIt').selectIt.remove();   //Removes element from page
                            $t.removeData('selectIt');   //Removes data in selectIt namespace
                            //Should the original element be made visible?
                        })
                    },

                    open : function(e) {
                        //console.log('open');
                        methods.close();
                        var $t=$(this),
                        $p=$t.parent(),
                        index=$p.prev().prop("selectedIndex"),
                        list=$p.find('ul.selectIt-list').show();
                        list.find('li.selectIt-selected').removeClass('selectIt-selected');
                        list.find('li').eq(index).addClass('selectIt-selected');
                        var top = 0 - list.find('li').eq(index).position().top;
                        list.css({top: top});
                        e.stopPropagation();    //Don't trigger documents click
                    },

                    //If this is all I am doing, can change to just using CSS psudo class.
                    mouseenter : function(e) {
                        $(this).addClass('selectIt-hover');
                    },
                    mouseleave : function(e) {
                        $(this).removeClass('selectIt-hover');
                    },

                    click : function(e) {
                        //console.log('click');
                        var $t=$(this);
                        if(!$t.hasClass('selectIt-selected'))
                        {
                            var $p=$t.parent(),
                            $pp=$p.parent(),
                            select=$pp.prev(),
                            index=$t.index(),
                            option=select.find('option').eq(index),
                            value=(value=option.val())?value:option.text();
                            select.val(value).prop('selectedIndex',index);
                            $pp.find('span.selectIt-text').text($t.text());
                            select.data('selectIt').settings.click.call(this,value);
                        }
                    },

                    close : function(e) {
                        //console.log('close');
                        $('div.selectIt').find('ul.selectIt-list').hide().parent();//.find('span.selectIt-text').show();
                    },

                    update : function(content) {alert('When will this type of method be used?');}
                };

                $.fn.selectIt = function(method) {
                    if ( methods[method] ) {
                        return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
                    } else if ( typeof method === 'object' || ! method ) {
                        return methods.init.apply( this, arguments );
                    } else {
                        $.error( 'Method ' +  method + ' does not exist on jQuery.selectIt' );
                    }    
                };

            })( jQuery );

            $(function(){
                $('select.mySelect').selectIt({
                    //'class' : 'whatever',
                    'extraWidth' : 0,
                    'click':function(value){
                        switch(value)
                        {
                            case 'first':console.log('Do something first');break;
                            case 'second':console.log('Do something second');break;
                            case 'third':console.log('Do something third');break;
                            default: console.log('Do something default');
                        }
                    }
                });

                $('#horizontalList button.destroy').click(function(){
                    $('#horizontalList select.mySelect').selectIt('destroy');
                });

               $('#verticalList button.destroy').click(function(){
                    $('#verticalList select.mySelect').selectIt('destroy');
                });
            });
        </script>
    </head>

    <body>

        <div id="horizontalList">
            <h1>Horizontal List</h1>

            <ul class="myList">
                <li>
                    <span class="label">Label</span>
                    <select class="mySelect">
                        <option value="first">First</option>
                        <option value="second" selected="selected">Second</option>
                        <option>Third</option>
                    </select>
                </li>
                <li>
                    <span class="label">Some Label</span>
                    <select class="mySelect">
                        <option value="first">First</option>
                        <option value="second" selected="selected">Second</option>
                        <option value="third">Third</option>
                    </select>
                </li>
                <li>
                    <span class="label">Some Label</span>
                    <select class="mySelect">
                        <option value="">First</option>
                        <option value="second">Second</option>
                        <option value="third">Third</option>
                    </select>
                </li>
            </ul>
            <button class="destroy">Destroy</button>
        </div>
        <div id="verticalList">
            <h1>Vertical List</h1>

            <ul class="myList">
                <li>
                    <span class="label">Label</span>
                    <select class="mySelect">
                        <option value="first">First</option>
                        <option value="second" selected="selected">Second</option>
                        <option>Third</option>
                    </select>
                </li>
                <li>
                    <span class="label">Some Label</span>
                    <select class="mySelect">
                        <option value="first">First</option>
                        <option value="second" selected="selected">Second</option>
                        <option value="third">Third</option>
                    </select>
                </li>
                <li>
                    <span class="label">Some Label</span>
                    <select class="mySelect">
                        <option value="">First</option>
                        <option value="second">Second</option>
                        <option value="third">Third</option>
                    </select>
                </li>
            </ul>
            <button class="destroy">Destroy</button>
        </div>
    </body> 
</html> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:02:20+00:00Added an answer on June 13, 2026 at 7:02 pm

    There is a line in your JQuery [line 173]:

    list.css({top: top});
    

    Insert this line below it:

    list.css({left: $(this).position().left});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating my first wordpress plugin. one of the questions i have is
I have a quick jQuery question: First, my HTML: <div id=taskinput> <form> <input id=taskinput-box
I see people creating HTML elements in jQuery in two different ways: $('<element>') and
Is there a general best practice for creating somewhat complex HTML elements in jQuery?
this is my first atempt of creating a jquery plugin so I thought id
I'm creating my first application and I have a window consisting of multiple subclasses
I am creating my first (!) database, and I have run into an issue
I am creating my first site from scratch using PHP, MySQL, CSS, HTML, and
I am writing a object-oriented jquery plugin. I have a class and I am
I'm creating a small jQuery plugin for my CMS that styles certain form input

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.