concept element in category jQuery

appears as: elements, elements, n element, element, element, The elements, The element
jQuery in Action, Third Edition

This is an excerpt from Manning's book jQuery in Action, Third Edition.

The most common use of jQuery() is to select elements from the DOM so you can apply some changes to them. In this case, it accepts two parameters: a selector and (optionally) a context. This function returns an object containing a collection of DOM elements that match the given criteria. But what’s a selector?

When CSS was introduced to web technologies in order to separate design from content, a way was needed to refer to groups of page elements from external style sheets. The method developed was to use selectors, which concisely represent elements based on their type, attributes, or position within the HTML document. Those familiar with XML might be familiar with XPath (more on this here: http://www.w3.org/TR/xpath20/) as a means to select elements within an XML document. CSS selectors represent an equally powerful concept but are tuned for use within HTML pages, are a bit more concise, and are generally considered easier to understand.

The methods for showing and hiding elements are pretty much what you’d expect: show(), to show the elements in a jQuery object, and hide(), to hide them. We’re going to delay presenting their formal syntax for reasons that will become clear in a bit; for now, let’s concentrate on using these methods with no arguments.

Let’s say that you want to conspicuously animate the removal of an element from the display. The animation you’ll use to accomplish this will make the element appear to drop off the page, disappearing from the display as it does so.

If you think about it for a moment, you can figure out that by adjusting the top position of the element, you can make it move down the page to simulate the drop; adjusting the opacity will make it seem to vanish as it does so. And finally, when all that’s done, you can remove the element from the display. You can accomplish this drop effect with the code in the following listing.

Listing 8.5. A custom drop animation

There’s a bit more going on here than in the previous custom effect. You once again iterate over the elements in the set, but this time adjusting the position and the opacity of the elements. To adjust the top value of an element relative to its original position, you first need to change its CSS position style property value to relative .

Then you specify a target opacity of 0 and a computed top value. You don’t want to move an element so far down the page that it moves below the window’s bottom; this could cause scroll bars to be displayed where none may have been before, possibly distracting users. You don’t want to draw their attention away from the animation—grabbing their attention is why you’re animating in the first place! Use the height and vertical position of the element, as well as the height of the window, to compute how far down the page the element should drop . Of course, this consideration makes sense only if the space between the elements and the bottom of the page is large enough.

When the animation is completed, you want to remove the element from the display, so specify a callback function that applies the non-animated hide() method to the element .

Method syntax: hide

hide(duration[, easing][, callback])
hide(options)
hide()
Causes the elements selected to become hidden. If called with no parameters, the operation takes place instantaneously by setting the display style property value of the elements to none. If a duration parameter is provided, the elements are hidden over a period of time by adjusting their width, height, and opacity downward to zero, at which time their display style property value is set to none to remove them from the display.
An optional easing function name can be passed to specify how the transition between the states is performed. An optional callback can be specified, and is invoked when the animation is complete. In its second version, you can provide an object containing some options to pass to the method (more on the properties available later).
Parameters
duration (Number|String) Specifies the duration of the effect as a number of milliseconds or as one of the predefined value strings: "slow" (same as passing 600), "normal" (same as passing 400), or "fast" (same as passing 200). If this parameter is omitted and a callback function is specified as the first parameter, the value of "normal" is assumed.
easing (String) Specifies an easing function name to use when performing the transition from the visible state to the hidden state. The easing functions specify the pace of the animation at different points while in execution. If an animation takes place but this parameter isn’t specified, it defaults to "swing". More about these functions in section 8.3.
callback (Function) A function invoked when the animation completes. No parameters are passed to this function, but the function context (this) is set to the element that was animated. The callback is fired for each element that undergoes animation.
options (Object) An optional set of options to pass to the method. The options available are shown in table 8.1.
Returns
The jQuery collection.
var $divs = $('div');
for (var element of $divs) {
   // Do something with element
}
jQuery UI in Action

This is an excerpt from Manning's book jQuery UI in Action.

  • Interactions are jQuery plugins that give the user the ability to interact with DOM elements. The draggable interaction allows users to drag elements around the screen, and the sortable interaction allows users to sort items in a list. We’ll cover interactions in chapter 5.
  • Now that the lists are populated, you turn them into widgets. The cursor and placeholder options should look familiar from the previous example, but the helper option is new. Whenever a drag starts on a sortable item, the element being dragged is referred to as a helper element, and is given a class name of ui-sortable-helper for styling purposes. By default, the helper is the sortable element itself, which corresponds to a helper option of "original". The helper option also accepts "clone", which clones the element and uses it as a helper, or a function that returns a new element to use as a helper. In this example, you use this option and create a new helper <div> :

    helper: function( event, item ) {
        return $( "<div>", { text: item.text() } );
    }

    Why did you do this? Because these sortable elements are <li> elements, by default they’re displayed with bullets next to them in the list—for example, • Banana. Dragging an element with the bullet looks a little odd, and creating a new <div> to use as a helper works around this.

    A DOM element can be initialized with multiple widgets. Although some combinations make no sense—for instance, an element that’s a dialog and a datepicker—some, such as draggable and resizable, can be quite useful.

    The resizable widget automatically adds an icon to the bottom right-hand side of the element. By default, resizable elements can be resized to the south, east, and southeast. The handles option lets you configure this behavior.

    The handles option is set to "e, s, se" by default, which explains the behavior you see. You can set the option to a comma-delimited string containing any of the following in any order: n, e, s, w, ne, se, sw, nw. You can also pass the string "all" to make an element that can be resized in any direction. The handles are shown in figure 5.9.

    Figure 5.9. The resizable widget lets you configure the directions the element can be resized with the handles option. The eight potential handles are shown on an element. The handles option also accepts "all", which uses all eight handles.
    Extending jQuery

    This is an excerpt from Manning's book Extending jQuery.

    jQuery selectors and filters allow you to identify and collect the elements from the web page that you wish to operate upon. Although standard selectors by node name, ID, and class are built into jQuery, there’s scope for adding pseudo-class selectors (extending the CSS-defined pseudo-classes) that allow you to filter a previous selection consistently and succinctly. You can also add set filters that are aware of the entire collection of previously selected elements and each one’s position within that list. Chapter 3 explains how to create these selectors.

    Alongside the various widgets provided by jQuery UI (discussed in the previous two chapters) are additional behaviors, including user interactions, such as draggable and droppable, and visual effects for presenting elements. These effects enhance the showing or hiding of elements on your web page or serve to highlight a particular element by animating various aspects of their appearance. Underlying the builtin effects is a core of functionality that’s useful in creating such animations. As you’d expect, you can add your own effects and have them integrate into jQuery UI along with the built-in ones.

    Figure 10.3. Break the element up into pieces, and move them separately toward the center.

    10.2.1. Imploding an element

    There’s an existing jQuery UI effect called explode that takes an element, breaks it up into several pieces, and then moves these away from each other while fading them out, to simulate it being blasted to pieces. When used to show a hidden element, this effect fades in the various pieces and reassembles the original element. To demonstrate how you can create your own effect, you can write a similar one that implodes an element rather than exploding it. Instead of the component pieces flying out when hiding an element, they’ll collapse into the center and fade away, as shown in figure 10.2. When showing an element with the imploding effect, the individual pieces will move out from the center and fade in to recreate the whole element.

    Listing 14.5. Previous pattern-matching rule
    function(value, element, param) { var re = param instanceof RegExp ? param : new RegExp(param); return this.optional(element) || re.test(value); },