Acting on Your Selection

You can act on your selection results immediately, within the same line of code. jQuery allows you to simply call a method of the returned selection (or another jQuery method) directly from the selection. Alternatively, you can store the results in a variable for use later in your code (as the preceding examples have shown).

This example finds the selection and then acts on it immediately, as a group, using the jQuery addClass method to add a CSS class to each item in the selection.

$('a').addClass('alink');

You can also store your selection for later use. When you do so, jQuery stores a reference (not a copy) to the elements in the selection. You can then use the returned jQuery object to call other jQuery methods, as ...

Get Microsoft Visual Studio 2015 Unleashed, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.