How can CSS and JavaScript be used to hide and display additional table rows for dynamic content in PHP?
To hide and display additional table rows for dynamic content in PHP, you can use CSS to initially hide the rows and JavaScript to toggle their visibility based on user interaction. By adding a CSS class to the rows and using JavaScript to toggle this class on click events, you can achieve the desired functionality.
<?php
// PHP code to generate dynamic table rows
echo '<table>';
for($i = 1; $i <= 10; $i++) {
echo '<tr class="hidden-row">';
echo '<td>Row ' . $i . '</td>';
echo '</tr>';
}
echo '</table>';
?>
<script>
// JavaScript code to toggle visibility of table rows
document.addEventListener('DOMContentLoaded', function() {
var rows = document.querySelectorAll('.hidden-row');
rows.forEach(function(row) {
row.addEventListener('click', function() {
this.classList.toggle('hidden-row');
});
});
});
</script>
<style>
/* CSS code to initially hide table rows */
.hidden-row {
display: none;
}
</style>