JavaScript jQuery Basics Understanding jQuery Events and DOM Traversal What is Traversal?

Silvia Ramos
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Silvia Ramos
Front End Web Development Techdegree Graduate 18,306 Points

prev()

prev() Hey, sorry but I didn't quite understand why she is using prev() to access the different buttons? Can someone explain this for "dummies"? haha I understood what the prev method is used for with those lists examples, but I can't get it right on this project?

Thank you! :)))

1 Answer

Steven Parker
Steven Parker
203,990 Points

The purpose of traversal is to get a reference to an element starting with a reference to another element.

The "prev()" method gets a reference to the element just before the one it is used on. So if you click on a button, the $(event.target) refers to the button itself, and using "prev()" on that refers to the span just before it:

<span style="display: none">Darth Vader is ... <span>  <!-- $(event.target).prev() -->
<button>Reveal Spoiler</button>                        <!-- $(event.target) -->

Does that help?