Skip to content

CODExperiments

Doing it MY way!

  • Home
  • 2011
  • April
  • 8
  • Dynamically load combobox using jQuery

Dynamically load combobox using jQuery

Posted on April 8, 2011January 2, 2015 By Zoon 1 Comment on Dynamically load combobox using jQuery
Programming

Giganews Newsgroups
Webpages easily contain a lot of duplicate content. This however, can hurt your Google pagerank. Duplicate content can also slow down your pages.
Currently I’m building a new site with some friends. This site has a sign-up form with all the usual fields like name, email, country and so on. Now, to make sure the new user enters a correct country, they are presented with a complete list of countries in the world. This list was, until a few days ago, loaded into all the pages on the site = bad for performance!

This is how I solved it:

First off, the signup form in hidden until the user decides to sign-up – clicking a button. When the form is presented the countries select-box is almost empty. It only contains one option “Please select a country”. When the user clicks the select box, all the countries are dynamically loaded through AJAX and inserted into the select box.

Below is how the countries are loaded using jQuery
[sourcecode language=”javascript”]
$(document).ready(function()
{
$(‘#country’).click(function()
{
// only load the list if not already loaded
if($(‘#country option’).size() > 1 )
return;
url = ‘http://www.zoon.dk/countries.json’;
$.getJSON(url, function(j)
{
var options = ”;
for (var i = 0; i < j.length; i++)
{
options += ‘<option value="’ + j[i][0] + ‘">’ + j[i][1] + ‘</option>’;
}
$("#country").html(options);
})
});
[/sourcecode]

As you can see, the countries list is in JSON format. Usually people store this kind of information in a database… but why? How often do we see a new country in the world? Why do you want the extra database overhead? I can’t come up with a single good reason… but perhaps you can?

You could choose to store the list in pure HTML, and just load it with
[sourcecode language=”javascript”]
$("#country").html(j);
[/sourcecode]

That would be even faster 🙂

Anyways, nothing beats a demo…
[ad name=”Google Adsense-1″]

Find a demo using this technique right here, and please leave a comment if you know of a better or faster way 🙂

Tags: AJAX HTML Javascript jQuery

Post navigation

❮ Previous Post: NZB Completion Checker
Next Post: NZB Completion Checker Update ❯

You may also like

Photo Mosaic
First and easy approach
March 21, 2010
Programming
Newshosting.com quick review
March 29, 2012
Programming
NZB Completion Checker 1.0 beta released!
February 1, 2012
Programming
NZB Completion Checker FINAL
June 1, 2012

One thought on “Dynamically load combobox using jQuery”

  1. Dan says:
    October 19, 2011 at 00:53

    You opened up a can of worms with that “extra database overhead” comment :). Typically, all static/lookup content is stored in DB lookup tables. If I hard coded any data like that in a file or HTML, my clients would fire me on the spot… it’s a no no. The middle tier (i.e. Java, .NET, etc.) would cache this kind of data a reload a specific interval. Caching is also another can of worms, but I’m just using it generally speaking. Having said that, I do agree with overhead, but a robust framework could handle situations like this. BTW, love the completion checker!

Leave a Reply

You must be logged in to post a comment.

Downloads

Get it while it's HOT:
NZBcc 1.2.2.0

Downloads
might be ad supported. If so, click in top right corner to continue when the counter reaches 0.

Support NZBcc



Sign up to Dropbox using this link

Archives

  • March 2016
  • September 2014
  • July 2014
  • January 2014
  • August 2013
  • January 2013
  • December 2012
  • November 2012
  • September 2012
  • August 2012
  • June 2012
  • May 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • August 2011
  • July 2011
  • April 2011
  • January 2011
  • December 2010
  • November 2010
  • August 2010
  • July 2010
  • April 2010
  • March 2010
  • January 2009

Tag Cloud

AJAX Android Articles C# Download FMV Giganews HLSL HTML HTML5 Javascript jQuery MPEG Decoding MySQL Netflix NZB NZBcc PHP SQL Usenet Windows 7 XNA

Copyright © 2025 CODExperiments.

Theme: Oceanly News Dark by ScriptsTown