I am trying to implement a modal dialog keyboard trap and can't seem to figure out why it isn't working as expected. I have included a jsfiddle below showing the code. In theory if a user is tabbing through the buttons using a keyboard once it reaches that last button it should jump to the first button. If a user is using shift and tabbing it should jump to the last button when the first button is reached. As you can see it actually skips the intended button in which I am directing focus. I would appreciate any help I can get. Thank you!