May 2017
Intermediate to advanced
448 pages
10h 10m
English
Even a task as simple as this can be complicated without jQuery at our disposal. In plain JavaScript, we could add the highlight class this way:
window.onload = function() { const divs = document.getElementsByTagName('div'); const hasClass = (elem, cls) => new RegExp(` ${cls} `).test(` ${elem.className} `); for (let div of divs) { if (hasClass(div, 'poem-stanza') && !hasClass(div, 'highlight')) { div.className += ' highlight'; } }};
Despite its length, this solution does not handle many of the situations that jQuery takes care of for us in Listing 1.2, such as:
Read now
Unlock full access