After finishing the excellent Object Oriented Javascript, I’m now working my way through Learning jQuery 4th Edition by Chaffer and Swedberg. Here’s my solution to exercises found in the end of Chapter 2 Selecting Elements.
- Add a class of special to all of the <li> elements at the second level of the nested list.
$('#selected-plays ul li').addClass('special')
- Add a class of year to all the table cells in the third column of a table.
$('tr').find('td:eq(2)').addClass('year')
- Add the class special to the first table row that has the word Tragedy
in it.$('td:contains(Tragedy)').first().parent().addClass('special')
- Challenge: Select all the list items (<li>s) containing
a link (<a>). Add the class afterlink to the sibling list items that follow
the ones selected.$('li a').not('[href^="mailto:"]').parent().nextAll().addClass('afterlink')
- Challenge: Add the class tragedy to the closest ancestor <ul> of
any .pdf link.$('a[href$=".pdf"]').closest('ul').addClass('tragedy')