My favourite jQuery expressions

This is purely for me and to remind myself of the frequently used jQuery expressions. Forgetfulness is a curse as well as a bliss!

$("#target option:first").attr('selected','selected');
  • populate select list dynamically. This simply replace html. If there is a better way, please let me know. I used an extension method, Html.ClientIdFor.
function(result) {
    var options = '';
    for(var i=0; i<result.length; i++) {
        options += '<option value="' + result[i].Id + '">' + result[i].Name + '</option>';
    }
    $('#<%= Html.ClientIdFor(m => m.Input.ProductCategoryId) %>').find('option').remove().end().html(options);
    $('#<%= Html.ClientIdFor(m => m.Input.ProductCategoryId) %> option:first').attr('selected', 'selected');
}
  • table and click on a link on table row
$('#result').delegate('a[name="lnkDescription"]', 'click', function(e) {
    e.preventDefault();

    var link = $(this).closest('tr').find('a[name="lnkDescription"]');

    ...
    });

})
My favourite jQuery expressions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s