How do I determine the state of a toggled element?

You can determine whether an element is collapsed or not by using the :visible and :hidden selectors. 1 2 3 var isVisible = $( "#myDiv" ).is( ":visible" ); var isHidden = $( "#myDiv" ).is( ":hidden" ); If you're simply acting on an element based on its visibility, just include :visible or :hidden in the selector … Continue reading

How do I disable/enable a form element?

You can enable or disable a form element using the .prop() method: 1 2 3 4 5 // Disable #x$( "#x" ).prop( "disabled", true ); // Enable #x$( "#x" ).prop( "disabled", false );

$ 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

The jQuery Object

When creating new elements (or selecting existing ones), jQuery returns the elements in a collection. Many developers new to jQuery assume that this collection is an array. It has a zero-indexed sequence of DOM elements, some familiar array functions, and a .length property, after all. Actually, the jQuery object is more complicated than that. link … 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

Selecting Elements

The most basic concept of jQuery is to "select some elements and do something with them." jQuery supports most CSS3 selectors, as well as some non-standard selectors. For a complete selector reference, visit the Selectors documentation on api.jquery.com. link Selecting Elements by ID 1 $( "#myId" ); // Note IDs must be unique per page. … Continue reading

Manipulating Elements

For complete documentation of jQuery manipulation methods, visit the Manipulation documentation on api.jquery.com. link Getting and Setting Information About ElementsThere are many ways to change an existing element. Among the most common tasks is changing the inner HTML or attribute of an element. jQuery offers simple, cross-browser methods for these sorts of manipulations. You can … 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

$( 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

Iterating over jQuery and non-jQuery Objects

jQuery provides an object iterator utility called $.each() as well as a jQuery collection iterator: .each(). These are not interchangeable. In addition, there are a couple of helpful methods called $.map() and .map() that can shortcut one of our common iteration use cases. link $.each()$.each() is a generic iterator function for looping over object, arrays, … Continue reading