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
Daniel Scalia
8,554 PointsHow can I add multiple spoil alerts on the same page?
I have tried to have multiple spoil alerts, but I can't figure out the problem. I click on first span/button and both spoil alerts comes up.... Here is my HTML:
<p class="answer1">
<!--Spoiler:-->
<span>What does Santa Cruz have that Pismo Beach doesn't?<br>Answer: Santa Cruz has Monarch Butterfly Tour. </span>
</p>
<p class="answer2">
<!--Spoiler:-->
<span>Which state has the largest population in the US?<br>Answer: California with 8 million people. </span>
</p>
Below is my jQuery code:
//Prevent spoilerphobes from seeing spoilers
//Solution: Hide spoilers and reveal them through user interaction
//1, Hide spoiler
$(".answer1 span").hide();
//2, Add a button
$(".answer1").append("<span id="museum">What does Santa Cruz have that Pismo Beach doesn't?<br> [press here for answer]</span>");
//3, When button pressed
$("span").click(function(){
//3.1, Show spoiler
$(".answer1 span").show();
//3.2, Get rid of button
$(this).remove();
});
//Prevent spoilerphobes from seeing spoilers
//Solution: Hide spoilers and reveal them through user interaction
//1, Hide spoiler
$(".answer2 span").hide();
//2, Add a button
$(".answer2").append("<span id="ca">Which state has the largest population in the US?<br> [press here for answer]</span>");
//3, When button pressed
$("span").click(function(){
//3.1, Show spoiler
$(".answer2 span").show();
//3.2, Get rid of button
$(this).remove();
});
I've added the id element to the span.. I tried this several times and it didn't work... Where should I add "#museum span"? Secondly, do I remove "this" from last line for remove() and replace with unique ID element?
1 Answer
Steven Parker
243,656 PointsYou could identify each button and spoiler uniquely.
Instead of attaching the same event handler to both based on the span tag, you could have separate handler attached to each one individually.
You can distinguish the elements several ways, including assigning each one a unique ID or class. Then you can use the unique feature as the selector, replacing the tag name entirely.
In future, be sure to format any code using the instructions from the Markdown Cheatsheet found below "Add an Answer".
Steven Parker
243,656 PointsIt looks like you revised your code.
So at this point, instead of:
$("span").click(...
... the first handler will be:
$("#museum").click(...
...and the second one will be
$("#ca").click(...
Also, where you have quotes inside a string, you must either use a different kind of quote (single instead of double, for example), or escape the inner quotes with a backslash, like this:
$(".answer1").append("<span id=\"museum\">What does Santa Cruz have ...
Daniel Scalia
8,554 PointsThank you sooo much!!!! I learned a lot from your feedback.
William Li
Courses Plus Student 26,868 PointsWilliam Li
Courses Plus Student 26,868 PointsMODERATOR NOTE: edited post for proper code syntax highlighting; in the future, please refer to this post https://teamtreehouse.com/community/posting-code-to-the-forum on how to use Markdown for posting code to the forum.