For a number of years I only want to show the data of the current year by default. That is different every year, so it is variable. I want to show en hide data of every year by checked de checkbox of each year. Checked will visible the data. Unchecked will hide the data. So only the current year is default visible. Here I found my answer in part. There an id of an element is used with a fixed value. What if #myDiv is a variable so in this topic $i?
Then how do I do $('div:not(#2021)').hide(); describe?
This is my code
<html>
<head>
<title>
Purchase
</title>
<script src= "https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<style type="text/css">
.selectt {
color: #fff;
padding: 30px;
display: compact;
margin-top: 30px;
width: 60%;
background: grey;
}
</style>
</head>
<body>
<center>
<table>
<?php for ($i = 2018; $i <= 2022; $i++) {
$currentYear = date("Y"); ?>
<tr>
<td>
<input type="checkbox" name="Checkbox" value= <?php echo $i; if($i == 2021) { ?> checked <?php } ?> > <?php echo $i; ?>
<div class= "<?php echo $i; ?> selectt" id = "<?php echo $i; ?>" >
This data belongs to this year</div>
</td>
</tr>
<?php } ?>
</table>
<script type="text/javascript">
$('div:not(#2021)').hide(); // hide everything that isn't #currentyear
$(document).ready(function() {
$('input[type="checkbox"]').click(function() {
var inputValue = $(this).attr("value");
$("." + inputValue).toggle();
});
});
</script>
</center>
</body>
</html>