Solutions to Exercises: Learning JQuery – Chapter 2 Selecting Elements

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.

  1. 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')
  2. Add a class of year to all the table cells in the third column of a table.
  3. Add the class special to the first table row that has the word Tragedy
    in it.

  4. 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')
  5. Challenge: Add the class tragedy to the closest ancestor <ul> of
    any .pdf link.