Archive

Posts Tagged ‘jquery’

How to sort a drop down list (select) using jQuery

July 15th, 2010 5 comments

Let’s assume we have a drop down list that it is initialized to display a list of option and programmatically you want to add or remove options. When you add options you might need to sort options in the drop down list based on the text or the value of each option.

Here is a select box with the list of Cities:

dropdown_list_cities

<select id="cities">
    <option value="0">Select City</option>
    <option value="2">Paris</option>
    <option value="3">London</option>
    <option value="4">Athens</option>
    <option value="5">Mexico</option>
</select>

In order to add a new city, let’s say Rome, and resort them you can do that client side using jQuery:

// Add a last option to the cities drop down list
$("#cities").append($('<option/>').attr('value', '1').text('Rome'));

// Sort by name
$("#cities").html($("#cities option").sort(function (a, b) {
    return a.text == b.text ? 0 : a.text < b.text ? -1 : 1;
}));

// Sort by Value
// If you don't convert a.value to integers javascript will assume
// that a.value and b.value are strings
$("#cities").html($("#cities option").sort(function (a, b) {
    var aValue = parseInt(a.value);
    var bValue = parseInt(b.value);
    return aValue == bValue ? 0 : aValue < bValue ? -1 : 1;
}));

// Reselect the first option
$('#cities').val('0');

What took me some time to figure out is that the value options should be converted to integers before sorting them as javascript will consider them as strings

Categories: Javascript, Web Design Tags: ,

Preload images with jQuery

May 15th, 2010 No comments

Sometimes you might want to preload some images used in your site to improve user experience. In photo galleries or other image-heavy sites preloading images help visitor enjoy faster loading times.


jQuery can help us with a few lines of code to achieve that:

//Function that preloads a defined list of images (arguments for the function).
 jQuery.preloadImages = function(){
  //Loop through the images
  for(var i = 0; i<arguments.length; i++){
    jQuery("<img>").attr("src", arguments[i]);
  }
}

// Call the function:
$.preloadImages("images/1.png", "images/2.png", "images/3.png", "images/4.jpg");
Categories: Javascript, Web Design Tags:

Storing Data using jQuery

May 14th, 2010 No comments

A lot of developers tend to store additional data in HTML attributes which are not meant to store data. In order to solve this issue jQuery offers a data method with which you can store any data you might need.

Read more…

Categories: Javascript, Web Design Tags:

ASP.NET validators and javascript can do beautiful things…

March 31st, 2010 4 comments

Here is a small recipe about making some things a little more beautiful using asp.net Validators and some javascript.

What do we need:

  • ASP.NET Validators like RequiredFieldValidator
  • jQuery
  • jQuery Tooltip
  • A nice tooltip image and some css

Read more…

Categories: ASP.NET Tags: , ,

jQuery 1.4 Released

January 15th, 2010 No comments

The jQuery Team announce the release of jQuery 1.4 in celebration of the projects 4th Birthday.
In the new version, the jQuery team has reduced the complexity of the code base and that lead to significant performance gains as the following diagram displays:
cheat sheetjQuery 1.4 - Performance gains in popular mthod calls

You can download the new version from the following links:

You can visit the site, the jQuery team launched for their 4th birthday, here

For the top 15 features of the new version you can read this article and have a look at the.


Categories: Javascript Tags: ,