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

  • SEARCH
  • Home
  • 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 7750137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:12:49+00:00 2026-06-01T11:12:49+00:00

Currently our dev team uses this pattern, but I can’t help but wonder if

  • 0

Currently our dev team uses this pattern, but I can’t help but wonder if there is a faster or more html-efficient way of accomplishing the same task.

HTML

<select id="myList" style="width: 400px;">
</select>
<script id="myListTemplate" type="text/x-jQuery-tmpl">
        <option value="${idField}">${name}</option>
</script>

And this is the Javascript:

function bindList(url) {
    callAjax(url, null, false, function (json) {
        $('#myList').children().remove();
        $('#myListTemplate').tmpl(json.d).appendTo('#myList');
    });
}
  • 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-01T11:12:51+00:00Added an answer on June 1, 2026 at 11:12 am

    This is a function I wrote to do just that. I’m not sure if it’s faster than jQuery Templates. It creates and appends the Option elements one at a time, which may be slower than Templates. I suspect that Templates builds the whole HTML string, and then creates the DOM elements all in one shot. That may be a faster. I suppose this function could be adjusted to do the same thing. I’ve worked with Templates, and I do find that this function is easier to consume for something a simple as populating a Select list, and it fits nicely into my utility.js file.

    Update: I updated my function to build the HTML first, and call append() only once. It actually runs much faster now. Thanks for posting this question, it’s nice to be able to optimize one’s own code.

    Consuming the function

     // you can easily pass in response.d from an AJAX call if it's JSON formatted
     var users = [ {id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Cindy'} ]
     setSelectOptions($('#selectList'), users, 'id', 'name');
    

    The function code

    // Fill a select list with options using an array of values as the data source
    // @param {String, Object} selectElement Reference to the select list to be modified, either the selector string, or the jQuery object itself
    // @param {Object} values An array of option values to use to fill the select list. May be an array of strings, or an array of hashes (associative arrays).
    // @param {String} [valueKey] If values is an array of hashes, this is the hashkey to the value parameter for the option element
    // @param {String} [textKey] If values is an array of hashes, this is the hashkey to the text parameter for the option element
    // @param {String} [defaultValue] The default value to select in the select list
    // @remark This function will remove any existing items in the select list
    // @remark If the values attribute is an array, then the options will use the array values for both the text and value.
    // @return {Boolean} false
    function setSelectOptions(selectElement, values, valueKey, textKey, defaultValue) {
    
        if (typeof (selectElement) == "string") {
            selectElement = $(selectElement);
        }
    
        selectElement.empty();
    
        if (typeof (values) == 'object') {
    
            if (values.length) {
    
                var type = typeof (values[0]);
                var html = "";
    
                if (type == 'object') {
                    // values is array of hashes
                    var optionElement = null;
    
                    $.each(values, function () {
                        html += '<option value="' + this[valueKey] + '">' + this[textKey] + '</option>';                    
                    });
    
                } else {
                    // array of strings
                    $.each(values, function () {
                        var value = this.toString();
                        html += '<option value="' + value + '">' + value + '</option>';                    
                    });
                }
    
                selectElement.append(html);
            }
    
            // select the defaultValue is one was passed in
            if (typeof defaultValue != 'undefined') {
                selectElement.children('option[value="' + defaultValue + '"]').attr('selected', 'selected');
            }
    
        }
    
        return false;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our dev shop currently uses Visual SourceSafe. We all know how that could end
Currently our website uses links to allow the user to change their locale. The
Currently our code uses a for-loop for filling a buffer holding a Unicode string
We (small team) currently have our Visual Studio projects on a network drive (no
I am currently involved embedded C++ development on am STM32 platform. Our team is
I'm trying to set up a TFS server for our small dev team, and
We're looking to iron out issues in our different dev/test/prod environments. Currently we have
Currently our developent enviroment consists of a Dev server, a Staging server and a
My team is currently building a new SaaS application for our company ( Amilia.com
for a team of 12 developers, can you help me determine a process and

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.