Optimize Selectors

Selector optimization is less important than it used to be, as more browsers implement document.querySelectorAll() and the burden of selection shifts from jQuery to the browser. However, there are still some tips to keep in mind when selector performance becomes a bottleneck. link jQuery ExtensionsWhen possible, avoid selectors that include jQuery extensions. These extensions cannot … Continue reading

Use Stylesheets for Changing CSS on Many Elements

If you’re changing the CSS of more than 20 elements using .css(), consider adding a style tag to the page instead for a nearly 60% increase in speed. 1 2 3 4 5 6 // Fine for up to 20 elements, slow after that:$( “a.swedberg” ).css( “color”, “#0769ad” ); // Much faster:$( “<style type=\”text/css\”>a.swedberg { … Continue reading

Advanced Plugin Concepts

link Provide Public Access to Default Plugin SettingsAn improvement we can, and should, make to the code above is to expose the default plugin settings. This is important because it makes it very easy for plugin users to override/customize the plugin with minimal code. And this is where we begin to take advantage of the … Continue reading

How to Create a Basic Plugin

Sometimes you want to make a piece of functionality available throughout your code. For example, perhaps you want a single method you can call on a jQuery selection that performs a series of operations on the selection. In this case, you may want to write a plugin. link How jQuery Works 101: jQuery Object MethodsBefore … Continue reading

Avoiding Conflicts with Other Libraries

The jQuery library and virtually all of its plugins are contained within the jQuery namespace. As a general rule, global objects are stored inside the jQuery namespace as well, so you shouldn’t get a clash between jQuery and any other library (like prototype.js, MooTools, or YUI). That said, there is one caveat: by default, jQuery … Continue reading

CSS, Styling, & Dimensions

jQuery includes a handy way to get and set CSS properties of elements: 1 2 3 4 5 // Getting CSS properties. $( “h1” ).css( “fontSize” ); // Returns a string such as “19px”. $( “h1” ).css( “font-size” ); // Also works. 1 2 3 4 5 6 7 8 9 // Setting CSS properties. … Continue reading

Data Methods

There’s often data about an element you want to store with the element. In plain JavaScript, you might do this by adding a property to the DOM element, but you’d have to deal with memory leaks in some browsers. jQuery offers a straightforward way to store data related to an element, and it manages the … Continue reading

Using jQuery UI with Bower

**Note:** This documentation refers to functionality made available in jQuery UI 1.11. Bower is a package manager for the Web. You can use Bower to download libraries like jQuery UI from the command line, without having to manually download each project from their respective sites. As an example, suppose we’re starting a new project and … Continue reading

Using the classes Option

As of the 1.12 release, the jQuery UI widget factory includes a means of managing CSS class names through the classes option. This article will give you an overview of how the classes option works, and discuss what you can do with it. link Syntax overviewThe classes option is used to map structural class names … Continue reading