⊗jqBsArV 7 of 113 menu

Writing a Set of Elements to a Variable with jQuery

You don't have to build the chain $('.www').html('!!!'), but first write the set of elements $('.www') into a variable (I called it elems), and then apply the method .html('!!!') to this variable. For example, our HTML code looks like this:

<p class="www">text</p> <p class="www">text</p> <p class="www">text</p> <p>text</p>

And this is what the code written in JavaScript will look like:

let elems = $('.www'); elems.html('!!!');

There is a convention that is desirable to use for convenience: the names of variables that contain a wrapped set of jQuery are usually started with a dollar. That is, in our case we need not elems, but $elems.

If you are confused by such a variable name - $elems - keep in mind that the dollar sign in JavaScript is a regular symbol and can be used anywhere. You can even make a function name consisting of a single character $, as is done in jQuery and some other libraries.

So, once again - if you want to write a jQuery array to a variable, then it is customary to start this variable with a dollar. This is not necessary, but it allows you to understand at a glance that this variable contains a group of elements and all jQuery methods are applicable to this variable.

Let's use this in our example with the HTML code below:

<p class="www">text</p> <p class="www">text</p> <p class="www">text</p> <p>text</p>

The JavaScript looks like this:

let $elems = $('.www'); $elems.html('!!!');

Similarly, to insert text, you can use the text method:

$('.www').text('!!!');

To all h3 using the text method, set the text '!!!'.

byenru