Checkbox limit-with partial id=^ s

Checkbox limit-with partial id=^ s

I’m having a small problem finding the right Jquery to get this to work. Building (pro-bono) a unique library checkout page for a non-profit.  They want books/titles listed under the subject(s) they cover, many titles cover multiple subjects. It’s a camelid org and have many titles that cover for example Training, health, breeding, etc.

Building the list using PHP, I create the unique IDs of each title and subject. The ID contains the sequential title number, the number of subjects represented and the number of that subject. Like ‘xyz120-9-5’. Select any ‘xyz120’ title checks (or uncheck) them all using ‘input[id^=’, that’s easy and works wonderful.  But I need to limit the number of titles to check to 5, and I’m lost. It’s easy to limit when there is one checkbox per title-subject.