So I am trying to manipulate the CSS using Javascipt, so that when the user clicks on the box, it displays the data I have for that specific month. I have managed to achieve it, however there just feel likes there is too much code, and that there can be a more efficient way to achieve what I am trying to do.
<div class="calander-container">
<div class="months">
<p class="months-text">January</p>
</div>
<div class="months">
<p class="months-text">February</p>
</div>
<div class="months">
<p class="months-text">March</p>
</div>
<div class="months">
<p class="months-text">April</p>
</div>
<div class="months">
<p class="months-text">May</p>
</div>
<div class="months">
<p class="months-text">June</p>
</div>
<div class="months">
<p class="months-text">July</p>
</div>
<div class="months">
<p class="months-text">August</p>
</div>
<div class="months">
<p class="months-text">September</p>
</div>
<div class="months">
<p class="months-text">October</p>
</div>
<div class="months">
<p class="months-text">November</p>
</div>
<div class="months">
<p class="months-text">December</p>
</div>
</div>
Above is the HTML used for the user to click the box for the script to then run.
document.querySelector('.calander-container').addEventListener('click', (e) => {
const months = e.target.closest('.months');
if (!months) {
return;
}
const thisIndex = [...months.parentElement.children].indexOf(months);
const rallyPrint = document.querySelectorAll('.rallyPrint');
rallyPrint.forEach((div) => {
div.style.display = 'none';
});
rallyPrint[thisIndex].style.display = 'block';
});
Above is the Javascript that I am using which works. However doing this function twelve times for each month just doesn't seem efficient. I feel as if there is any easier way to accomplish this. Below is the PHP I am using if that is any help.
if($row['month'] === 'January'){
echo "<div class='rallyPrint'>";
echo "<h2>" . $row['rallyVenue'] . " in " . $row['month'] . " " . $row['year'] . "</h2>";
echo "<h4>Event: ". $row['rallyEvent'] . " </h4>";
echo "<h3>Your Marshall(s): " . $row['rallyMarsh'] . "</h3>";
echo "<h4>When? ". $row['rallyDate'] . " </h4>";
echo "<p>" . $row['rallyDesc'] . "</p>";
echo "<p>How much? ". $row['rallyCost'] . " </p>";
echo "<p>How long? ". $row['rallyNights'] . " Nights</p>";
echo "<p>Pitch Limit? ". $row['pitchLimit'] . "</p>";
echo "<p>Phone Number: 0". $row['phoneNo'] . " </p>";
echo "<p>Email: <a href='mailto:". $row['email'] . "'> ". $row['email'] ."</a></p>";
echo "<p>Please make sure you to contact ". $row['rallyMarsh'] . " for more information.</p>";
if(isset($_SESSION['loggedin'])){
echo "<a href='' id='". $row['rallyId'] . "' class='trash'>Delete</a>";
}
echo "</div><br>";
}