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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access