Dynamically handling jQuery UI css states

I recently completed a project using jQuery and jQuery UI, and found them to both be incredible tools for creating dynamic websites.  One limitation with the jQuery UI css library was that while classes are provided for hover and focus states they are not driven by css pseudo selectors, meaning you have to dynamically add the classes yourself.  Using the jQuery live bind function this can be done with the following code snippet I wrote….

$(function() {/* Bind  functions for handling css class to jQuery events */

$(“.ui-state-default:not(.ui-state-disabled)”).live(“mouseover”, function() {

$(this).addClass(“ui-state-hover”);

});

$(“.ui-state-default:not(.ui-state-disabled)”).live(“mouseout”, function() {

$(this).removeClass(“ui-state-hover”).removeClass(“ui-state-focus”);

});

$(“.ui-state-default:not(.ui-state-disabled)”).live(“mousedown”, function() {

$(this).addClass(“ui-state-focus”);

});

$(“.ui-state-default:not(.ui-state-disabled)”).live(“mouseup”, function() {

$(this).removeClass(“ui-state-focus”);

});

$(“.ui-state-default:not(.ui-state-disabled)”).live(“focus”, function() {

$(this).addClass(“ui-state-hover”);

});

$(“.ui-state-default:not(.ui-state-disabled)”).live(“blur”, function() {

$(this).removeClass(“ui-state-hover”);

$(this).removeClass(“ui-state-focus”);

});

$(“.ui-state-default:not(.ui-state-disabled)”).live(“keydown”, function() {

$(this).addClass(“ui-state-focus”);

});

$(“.ui-state-default:not(.ui-state-disabled)”).live(“keyup”, function() {

$(this).removeClass(“ui-state-focus”);

});

});

Basically, you are selecting the elements on the page with the css class “ui-state-default” and add or removing the appropriate functions to change the css classes based on the triggered jQuery event.

Advertisements

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