Let's now write a function that takes a group of elements selector and their new text as parameters. Let this function set new text to all elements, matching for the selector.
Let's implement the described function:
function setText(selector, text) {
let elems = document.querySelectorAll(selector);
for (let elem of elems) {
elem.textContent = text;
}
}
Let's test it for a group of the following elements:
<p class="elem"></p>
<p class="elem"></p>
<p class="elem"></p>
Set new text for all elements with class elem
:
setText('.elem', 'text');
Make the appendText
function that takes a
selector as the first parameter and text as the
second. Make this function append text to the end
of passed elements.