How do I get the text value of a selected option?

Select elements typically have two values that you want to access. First there’s the value to be sent to the server, which is easy: 1 2 $( “#myselect” ).val();// => 1 The second is the text value of the select. For example, using the following select box: 1 2 3 4 5 6 7 <select … Continue reading

How do I pull a native DOM element from a jQuery object?

A jQuery object is an array-like wrapper around one or more DOM elements. To get a reference to the actual DOM elements (instead of the jQuery object), you have two options. The first (and fastest) method is to use array notation: 1 $( “#foo” )[ 0 ]; // Equivalent to document.getElementById( “foo” ) The second … 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

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

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

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

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

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

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

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