My favourite jQuery expressions

less than 1 minute read

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

[sourcecode language="javascript"]
$("#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.

[sourcecode language="javascript"]
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

[sourcecode language="javascript"]
$('#result').delegate('a[name="lnkDescription"]', 'click', function(e) {

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