Adding Elements to the Current Set in jQuery
Sometimes there are situations when we already have a set of methods, we have performed some operations on it and now we would like to add more elements to this set and perform some more operations on this common set. This is done using the add method.
Take, for example, the following HTML code:
<div>ddd</div>
<h1>hhh</h1>
<p>ppp</p>
<div id="test"><h2>hhh</h2></div>
<p>ppp</p>
<h2>hhh</h2>
<p>ppp</p>
Let's find all the paragraphs, add the text '!' to the end of them, then add headings h2 to the found paragraphs, and only those that are inside the element #test, and at the same time set the color red for the selected headings and paragraphs:
$('p').append('!').add('#test h2').css('color', 'red');
Find all paragraphs p with class www, add text '!' to their beginning and text '!!' to their end, then add headings h2 to these paragraphs and color these paragraphs and headings red.