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 8786923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:45:24+00:00 2026-06-13T21:45:24+00:00

I have a function that I call multiple times in my projects: function fillSelect(select)

  • 0

I have a function that I call multiple times in my projects:

function fillSelect(select) {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "Data.asmx/Status",
        dataType: "json",
        async: true,
        success: function(data) {
            $.each(data.d, function(i) {
                select.append('<option value=' + data.d[i].value + '>' + data.d[i].name + '</option>');
            });
        },
        error: function(result) {
            alert("Error occured. Contact admin");
        }
    });
}

Then in my code I’m using this like so:

fillSelect($('select#status1'));
fillSelect($('select#status2'));
fillSelect($('select#status3'));

What I would like to do is to convert my function into plugin, so I would be able to call it as so:

$('select#status1, select#status2, select#status3').fillSelect();

Using http://starter.pixelgraphics.us/ I’ve generated empty schema:

(function($) {
    $.ajaxSelect = function(el, select, options) {
        // To avoid scope issues, use 'base' instead of 'this'
        // to reference this class from internal events and functions.
        var base = this;

        // Access to jQuery and DOM versions of element
        base.$el = $(el);
        base.el = el;

        // Add a reverse reference to the DOM object
        base.$el.data("ajaxSelect", base);

        base.init = function() {
            base.select = select;

            base.options = $.extend({}, $.ajaxSelect.defaultOptions, options);

            // Put your initialization code here
        };

        // Sample Function, Uncomment to use
        // base.functionName = function(paramaters){
        // 
        // };

        // Run initializer
        base.init();
    };

    $.ajaxSelect.defaultOptions = {
        clear: false //append to select or replace current items
    };

    $.fn.ajaxSelect = function(select, options) {
        return this.each(function() {
            (new $.ajaxSelect(this, select, options));
        });
    };

})(jQuery);

but I don’t know how to fill it.
What I would like to do is to call sever ones and then fill as many select items as I put in parameters.
Is all that code really necessary for such a small plugin?

I know that there are probably some plugins that this functionality, but I would like to create my own plugin, just to learn a bit more 🙂

  • 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-13T21:45:25+00:00Added an answer on June 13, 2026 at 9:45 pm

    You don’t need all that boiler plate you could do as below

    $.fn.fill = function fillSelect(options) {
       var self = this; 
       options = $.extend({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Data.asmx/Status",
                dataType: "json",
                async: true,
                success: function(data) {
                   var list = "";
                   $.each(data.d, function(i) {
                         list += '<option value=' 
                                  + data.d[i].value + '>' 
                                  + data.d[i].name 
                                  + '</option>';
                      });
                   self.filter("select").each(function(){
                       $(this).append(list);
                   });
                 },
                error: function(result) {
                    alert("Error occured. Contact admin");
                }
              },options);
        $.ajax(options);
        return this;
    }
    

    the first thing to notice that the function is added to the jQuery prototype/$.fn. Then the success handler have been changed so that all selected elements will be handled and lastly the selection is returned to make chaining possible, as this is usually expect when using jQuery.

    The above code will append the same options to all selected “select” elements only. If you select something else the options will not be appended to those elements.

    I’ve changed the signature to accept an options element. In the above version there’s default vesrion equaling your ajax options. If other values are supplied, they will override the default ones if a default exist. If a default does not exist the values will be added to the options object

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ajax call that fires multiple times but is called once. function
I have a function in R that I call multiple times. I want to
I have a single xml parsing function that I'm trying to call multiple times
I have a function that needs to call a virtual method many times in
Say I have a function foo that I want to call n times. In
I have an AS3 program that calls a function multiple times. The function must
I have a call back, that I call back multiple times ( recursively via
I have function getCartItems in cart.js and I want to call that function in
I have a function that i need to call on iframe mousemove(). But i
I would like to have a function that can wrap any other function call.

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.