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

Working with Selections

link Getters & SettersSome jQuery methods can be used to either assign or read some value on a selection. When the method is called with a value as an argument, it's referred to as a setter because it sets (or assigns) that value. When the method is called with no argument, it gets (or reads) … Continue reading

Utility Methods

jQuery offers several utility methods in the $ namespace. These methods are helpful for accomplishing routine programming tasks. For a complete reference on jQuery utility methods, visit the utilities documentation on api.jquery.com. Below are examples of a few of the utility methods: link $.trim()Removes leading and trailing whitespace: 1 2 // Returns "lots of extra … 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 jQuery’s .index() Function

.index() is a method on jQuery objects that's generally used to search for a given element within the jQuery object that it's called on. This method has four different signatures with different semantics that can be confusing. This article covers details about how to understand the way .index() works with each signature. link .index() with … Continue reading

Traversing

Once you've made an initial selection with jQuery, you can traverse deeper into what was just selected. Traversing can be broken down into three basic parts: parents, children, and siblings. jQuery has an abundance of easy-to-use methods for all these parts. Notice that each of these methods can optionally be passed string selectors, and some … Continue reading

Triggering Event Handlers

jQuery provides a way to trigger the event handlers bound to an element without any user interaction via the .trigger() method. link What handlers can be .trigger()'d?jQuery's event handling system is a layer on top of native browser events. When an event handler is added using .on( "click", function() {…} ), it can be triggered … Continue reading

Using jQuery UI ThemeRoller

link About ThemeRollerThemeRoller is a web app that offers a fun and intuitive interface for designing and downloading custom themes for jQuery UI. You can find ThemeRoller in the "Themes" section of the jQuery UI site, or by following this link: jQuery UI ThemeRoller link The ThemeRoller InterfaceThe interface for ThemeRoller is categorized into panels … 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

Finding & Evaluating Plugins

One of the most celebrated aspects of jQuery is its extensive plugin ecosystem. From table sorting to form validation to autocompletion – if there's a need for it, chances are good that someone has written a plugin for it. The quality of jQuery plugins varies widely. Many plugins are extensively tested and well-maintained, but others … Continue reading

$( document ).ready()

A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute. Code included inside $( window ).on( "load", function() { … }) will run … Continue reading

$ vs $()

Until now, we've been dealing entirely with methods that are called on a jQuery object. For example: 1 $( "h1" ).remove(); Most jQuery methods are called on jQuery objects as shown above; these methods are said to be part of the $.fn namespace, or the "jQuery prototype," and are best thought of as jQuery object … Continue reading

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

Custom Effects with .animate()

jQuery makes it possible to animate arbitrary CSS properties via the .animate() method. The .animate() method lets you animate to a set value, or to a value relative to the current value. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // Custom effects with .animate()$( "div.funtimes" ).animate( { … Continue reading

Widget Method Invocation

Widgets created with the widget factory use methods to change their state and perform actions after initialization. There are two ways widget methods can be invoked – through the plugin created by the widget factory, or by invoking the method on the element's instance object. link Plugin InvocationTo invoke a method using the widget's plugin, … Continue reading

About jQuery

Getting started with jQuery can be easy or challenging, depending on your experience with JavaScript, HTML, CSS, and programming concepts in general. In addition to these articles, you can read about the history of jQuery and the licensing terms that apply to jQuery projects. You can also make a donation to help the jQuery team … 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

Append Outside of Loops

Touching the DOM comes at a cost. If you're appending a lot of elements to the DOM, you will want to append them all at once, rather than one at a time. This is a common problem when appending elements within a loop. 1 2 3 4 5 6 7 $.each( myArray, function( i, item … Continue reading

Detach Elements to Work with Them

The DOM is slow; you want to avoid manipulating it as much as possible. jQuery introduced detach() in version 1.4 to help address this issue, allowing you to remove an element from the DOM while you work with it. 1 2 3 4 5 6 7 8 var table = $( "#myTable" );var parent = … Continue reading