Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS jQuery Basics (2014) Creating a Spoiler Revealer Perfect

Alice Huang
Alice Huang
20,335 Points

The order of "$(this).prev().show(); " and "$(this).remove();"

Just out of curiosity, would the code still work if the order was switched, considering, logically speaking, the button wouldn't have existed to be selected after it'd been removed?

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Alice,

No, this wouldn't work in the reverse order and that's due to how browsers handle DOM elements when they're removed, in order for the prev method to work a valid object must be used in the jQuery selector before it but if this is no longer in the DOM jQuery will receive an error stating the element object is null which will result in the prev method failing along with the show method.

Hope that helps.