I'm coding with jQuery at the moment. I learned about .replaceWith(); earlier this morning. When I click on my button, it switches to the "Future Dates", and the button changes to "Current Dates". It also switches the first listed item from "Mini Sprint" to "Snowball Sprint". But when I click my button back to "Current Dates", it no longer allows me to toggle it. Can someone explain why this isn't displaying correctly for me? Thank you.
$(document).ready(function() {
//Hide everything with the class of races.
$(".races").hide();
//First image should show and hide.
$("#first").click(function() {
$("#5k").toggle();
$(".firstmini").hide();
$(".firstmini2").hide();
$(".firstmini3").hide();
});
//5k/10k Events' firstlist should show its children when clicked.
$("#firstlist").click(function() {
$(".firstmini").toggle();
$(".firstmini2").hide();
$(".firstmini3").hide();
});
//5k/10k Events' firstlist2 should show its children when clicked.
$("#firstlist2").click(function() {
$(".firstmini2").toggle();
$(".firstmini").hide();
$(".firstmini3").hide();
});
//5k/10k Events' firstlist3 should show its childern when clicked.
$("#firstlist3").click(function() {
$(".firstmini3").toggle();
$(".firstmini").hide();
$(".firstmini2").hide();
});
$(".change").click(function() {
var $this = $(this);
$this.toggleClass("Future Dates");
if ($this.hasClass("Future Dates")) {
$this.text("Current Dates");
$("#firstlist").replaceWith("<li id='firstlist'>Snowball Sprint</li>");
} else {
$this.text("Future Dates");
$("#firstlist").replaceWith("<li id='firstlist'>Mini Sprint</li>");
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="change">Future Dates</button>
<div class="race_box">
<img id="first" src="images/run1.jpg" />
<br />
<figcaption>5k/10k Events</figcaption>
<div class="races" id="5k">
<h3>5k/10 Events</h3>
<ul>
<li id="firstlist">Mini Sprint
<ul class="firstmini">
<li>10/30/17 Memorial Park Appleton</li>
</ul>
</li>
<li id="firstlist2">Iron Horse
<ul class="firstmini2">
<li>11/06/17 Bay Beach Park Green Bay</li>
</ul>
</li>
<li id="firstlist3">Twilight Trail
<ul class="firstmini3">
<li>11/13/17 River's Edge Park Wrightstown</li>
</ul>
</li>
</ul>
</div>
</div>
` inside it.