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 trialSteve Fan
8,218 PointsHow could previous element of button is the span?
We have the code:
$(this).prev().show(); in $("button").click(function){}
As 1st assumption, I understand, "this" stands for button. It means $(this).prev().show(); this line of code target the previous element of "button".
However, after we use "append" to add the button. It could be before attribute "span". Here is HTML code:
<p class="spoiler"> <!--Spoiler:--> <span>Luke and Leia are siblings. Ew.</span> </p>
So how It, "span" could be previous element of "button". I understand the opposite way: "button" is previous element of "span".
So why this code works $(this).prev().show();
Or in 2nd assumption, "append" add the "button" element AFTER <span> which is already there.
If It is in this case, I can understand how pre() work.
Could someone give some glues/comments about my thinking.
1 Answer
Steven Parker
231,269 PointsWhatever you append() goes at the end of the element.
So if a paragraph already contains a span, and you append() a button to the paragraph, then inside the paragraph the span will come first, and the button will come after. The button will not come before the span. Check the jQuery documentation for more details.
Also, be aware that span is a separate element, it is not an attribute of the paragraph.
Steve Fan
8,218 PointsSteve Fan
8,218 PointsThank you Steven, your answer is really comprehensive.